删除其他进程正在使用的文件

时间:2021-04-18 18:06:28

I'm trying to programmically delete a file, but the file is apparently being used by another process (which happens to be my program). Basically, the program loads images from a folder by using FromUri to create a Bitmap, which is then loaded into an Image array, which in turn becomes the child of a stackpanel. Not very efficient, but it works.

我正在尝试以编程方式删除文件,但该文件显然正在被另一个进程使用(这恰好是我的程序)。基本上,程序通过使用FromUri来创建一个Bitmap来加载一个文件夹中的图像,然后将其加载到一个Image数组中,该数组又成为一个stackpanel的子代。不是很有效,但它的工作原理。

I've tried clearing the stackpanel's children, and making the images in the array null, but I'm still getting the IOException telling me that the file is being used by another process.

我已经尝试清除stackpanel的子节点,并使数组中的图像为null,但我仍然得到IOException告诉我该文件正由另一个进程使用。

Is there some other way to remove the file from my application's processes?

有没有其他方法从我的应用程序的进程中删除该文件?

7 个解决方案

#1


16  

In order to release an image file after loading, you have to create your images by setting the BitmapCacheOption.OnLoad flag. One way to do this would be this:

要在加载后释放图像文件,您必须通过设置BitmapCacheOption.OnLoad标志来创建图像。一种方法是这样做:

string filename = ...
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(filename);
image.EndInit();

Although setting BitmapCacheOption.OnLoad works on a BitmapImage that is loaded from a local file Uri, this is afaik nowhere documented. Therefore a probably better or safer way is to load the image from a FileStream, by setting the StreamSource property instead of UriSource:

虽然设置BitmapCacheOption.OnLoad可以在从本地文件Uri加载的BitmapImage上运行,但这一点无法记录。因此,通过设置StreamSource属性而不是UriSource,可能更好或更安全的方法是从FileStream加载图像:

string filename = ...
BitmapImage image = new BitmapImage();

using (var stream =
    new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.StreamSource = stream;
    image.EndInit();
}

#2


70  

it may be Garbage Collection issue.

它可能是垃圾收集问题。

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers(); 
File.Delete(picturePath);

#3


5  

Another way is to delete file. Load your file using FileStream class and release an file through stream.Dispose(); it will never give you the Exception "The process cannot access the file '' because it is being used by another process."

另一种方法是删除文件。使用FileStream类加载文件并通过stream.Dispose();释放文件。它永远不会给你异常“进程无法访问文件”,因为它正被另一个进程使用。

using (FileStream stream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read))
{
    pictureBox1.Image = Image.FromStream(stream);
     stream.Dispose();
}

 // delete your file.

 File.Delete(delpath);

#4


0  

Sorry my bad English. I hope to help:

抱歉,我的英语不好。我希望能帮到你:

var uploadedFile = Request.Files[0]; //Get file
var fileName = Path.GetFileName(uploadedFile.FileName);  //get file name
string fileSavePath = Server.MapPath(fileName); //get path
uploadedFile.SaveAs(fileSavePath); //saving file
FileInfo info = new FileInfo(fileSavePath);//get info file
//the problem ocurred because this, 
FileStream s = new FileStream(fileSavePath, FileMode.Open); //openning stream, them file in use by a process
System.IO.File.Delete(fileSavePath); //Generete a error
//problem solved here...
s.Close();
s.Dispose();
System.IO.File.Delete(fileSavePath); //File deletad sucessfully!

#5


0  

from my point of view, the general answer would be to backtrack the object that is keeping an open process with the file you want to delete. In my case it was a MailMessage, but just as well it might be a thread, filestream, etc, which you need to dispose.

从我的角度来看,一般的答案是回溯使用您要删除的文件保持打开进程的对象。在我的例子中,它是一个MailMessage,但它也可能是一个线程,文件流等,你需要处理它。

#6


0  

I had the similar issue. The only difference was that I was using Binding(MVVM Pattern). Nothing much worked then I removed everything and tried with Binding Mode=OneWay along with GC.Collect() before calling File.Delete(path) and it worked finally.

我有类似的问题。唯一的区别是我使用Binding(MVVM模式)。没有什么工作,然后我删除了所有东西并尝试使用Binding Mode = OneWay以及GC.Collect(),然后调用File.Delete(路径)并最终工作。

#7


0  

I had the same issue. The problem I had was with the openFileDialog and saveFileDialog having the following set:

我遇到过同样的问题。我遇到的问题是openFileDialog和saveFileDialog具有以下设置:

MyDialog.AutoUpgradeEnabled = false;

MyDialog.AutoUpgradeEnabled = false;

I commented out that line and it was resolved.

我评论了这条线并且它已经解决了。

#1


16  

In order to release an image file after loading, you have to create your images by setting the BitmapCacheOption.OnLoad flag. One way to do this would be this:

要在加载后释放图像文件,您必须通过设置BitmapCacheOption.OnLoad标志来创建图像。一种方法是这样做:

string filename = ...
BitmapImage image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(filename);
image.EndInit();

Although setting BitmapCacheOption.OnLoad works on a BitmapImage that is loaded from a local file Uri, this is afaik nowhere documented. Therefore a probably better or safer way is to load the image from a FileStream, by setting the StreamSource property instead of UriSource:

虽然设置BitmapCacheOption.OnLoad可以在从本地文件Uri加载的BitmapImage上运行,但这一点无法记录。因此,通过设置StreamSource属性而不是UriSource,可能更好或更安全的方法是从FileStream加载图像:

string filename = ...
BitmapImage image = new BitmapImage();

using (var stream =
    new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    image.BeginInit();
    image.CacheOption = BitmapCacheOption.OnLoad;
    image.StreamSource = stream;
    image.EndInit();
}

#2


70  

it may be Garbage Collection issue.

它可能是垃圾收集问题。

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers(); 
File.Delete(picturePath);

#3


5  

Another way is to delete file. Load your file using FileStream class and release an file through stream.Dispose(); it will never give you the Exception "The process cannot access the file '' because it is being used by another process."

另一种方法是删除文件。使用FileStream类加载文件并通过stream.Dispose();释放文件。它永远不会给你异常“进程无法访问文件”,因为它正被另一个进程使用。

using (FileStream stream = new FileStream("test.jpg", FileMode.Open, FileAccess.Read))
{
    pictureBox1.Image = Image.FromStream(stream);
     stream.Dispose();
}

 // delete your file.

 File.Delete(delpath);

#4


0  

Sorry my bad English. I hope to help:

抱歉,我的英语不好。我希望能帮到你:

var uploadedFile = Request.Files[0]; //Get file
var fileName = Path.GetFileName(uploadedFile.FileName);  //get file name
string fileSavePath = Server.MapPath(fileName); //get path
uploadedFile.SaveAs(fileSavePath); //saving file
FileInfo info = new FileInfo(fileSavePath);//get info file
//the problem ocurred because this, 
FileStream s = new FileStream(fileSavePath, FileMode.Open); //openning stream, them file in use by a process
System.IO.File.Delete(fileSavePath); //Generete a error
//problem solved here...
s.Close();
s.Dispose();
System.IO.File.Delete(fileSavePath); //File deletad sucessfully!

#5


0  

from my point of view, the general answer would be to backtrack the object that is keeping an open process with the file you want to delete. In my case it was a MailMessage, but just as well it might be a thread, filestream, etc, which you need to dispose.

从我的角度来看,一般的答案是回溯使用您要删除的文件保持打开进程的对象。在我的例子中,它是一个MailMessage,但它也可能是一个线程,文件流等,你需要处理它。

#6


0  

I had the similar issue. The only difference was that I was using Binding(MVVM Pattern). Nothing much worked then I removed everything and tried with Binding Mode=OneWay along with GC.Collect() before calling File.Delete(path) and it worked finally.

我有类似的问题。唯一的区别是我使用Binding(MVVM模式)。没有什么工作,然后我删除了所有东西并尝试使用Binding Mode = OneWay以及GC.Collect(),然后调用File.Delete(路径)并最终工作。

#7


0  

I had the same issue. The problem I had was with the openFileDialog and saveFileDialog having the following set:

我遇到过同样的问题。我遇到的问题是openFileDialog和saveFileDialog具有以下设置:

MyDialog.AutoUpgradeEnabled = false;

MyDialog.AutoUpgradeEnabled = false;

I commented out that line and it was resolved.

我评论了这条线并且它已经解决了。