在运行时删除文件夹

编程入门 行业动态 更新时间:2024-10-28 07:20:46
本文介绍了在运行时删除文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在运行时删除文件,但显示的错误是无法访问文件,因为它被另一个进程使用或无法删除只读文件.A'.dll'文件附加到码。如果'.dll'文件未附加,则可以正常工作。 有没有办法解决上述错误。

I want to delete a file on run time but the error showing is "cannot access a file because it is used by another process" or "cant delete a read only file".A '.dll' file is attached to the code. If '.dll' file is not attached it works fine. Is there any way to solve the above error.

if ((Directory.Exists(@"C:\\Users\\Anand\\Desktop\\temp_image))) { Directory.Delete(@"C:\\Users\\Anand\\Desktop\\temp_image, true); }

这里temp_image是我要删除的文件夹。

Here temp_image is the folder, I want to delete.

Here '.dll' is camera library which is attached to another form. I want to create the temporary folder at runtime which capture images temporary and after closing the application temporary folder(temp_folder) is deleted.Explain....

我的尝试: 上面提到的代码

What I have tried: Code mentioned above

推荐答案

因为它名为temp_image,它可能包含你的应用程序正在使用的图片 - 这可能是问题。 如果你使用Image.FromFile或类似的,然后它获取文件的锁定,直到图像被处置(这是所有文档: Image.FromFile方法(字符串)(System.Drawing) [ ^ ] - 请参阅备注部分) 所以看看在代码的其余部分,以及如何处理图像和文件 - 在您更正之前,您无法删除该文件夹,因为您的应用程序正在使用其中的文件。 Because it's named "temp_image" it's likely that it contains pictures your application is using - and that's probably the problem. If you use Image.FromFile or similar, then it acquires a lock on the file until the Image is Disposed (it's all in teh documentation: Image.FromFile Method (String) (System.Drawing)[^] - see the Remarks section) So look at the rest of your code, and how you are handling the images and files - until you have corrected that, you can't delete the folder as your application is using files inside it.

首先使用'Process Explorer' [ ^ ]查看哪个进程正在使用该文件。如果该过程结果是您项目的可执行文件,请尝试执行以下检查 1.确保正确关闭并(或)处置所有System.IO。 StreamReader(s)和System.IO.StreamWriter(s) 2.确保类库(.dll)中没有代码在运行时访问文件 希望您找到这个有用的 Firstly use 'Process Explorer'[^] to see which process is using the file. If the process turns out to be your project's executable file try to do the following checks 1. Make sure you properly close and (or) dispose all System.IO.StreamReader(s) and System.IO.StreamWriter(s) 2. Make sure there's no code in your class library(.dll) accessing the file at runtime Hope you find this useful

更多推荐

在运行时删除文件夹

本文发布于:2023-11-13 09:14:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583977.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!