在运行时删除文件夹 [英] Delete a folder at run time

查看:84
本文介绍了在运行时删除文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时删除文件,但显示的错误是无法访问文件,因为它被另一个进程使用或无法删除只读文件.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


这篇关于在运行时删除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆