C# 删除另一个进程正在使用的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13262548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete a file being used by another process
提问by bramco
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 创建位图从文件夹加载图像,然后将其加载到图像数组中,该数组又成为堆栈面板的子项。效率不高,但确实有效。
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.
我已经尝试清除堆栈面板的子项,并使数组中的图像为空,但我仍然收到 IOException,告诉我该文件正在被另一个进程使用。
Is there some other way to remove the file from my application's processes?
还有其他方法可以从我的应用程序进程中删除文件吗?
采纳答案by Clemens
In order to release an image file after loading, you have to create your images by setting the BitmapCacheOption.OnLoadflag. 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.OnLoadworks 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 StreamSourceproperty instead of UriSource:
尽管设置BitmapCacheOption.OnLoad适用于从本地文件 Uri 加载的 BitmapImage,但这是无处记录的。因此,可能更好或更安全的方法是通过设置StreamSource属性而不是从 FileStream 加载图像UriSource:
string filename = ...
BitmapImage image = new BitmapImage();
using (var stream = File.OpenRead(filename))
{
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.StreamSource = stream;
image.EndInit();
}
回答by Shahid Manzoor
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);
回答by kplshrm7
it may be Garbage Collection issue.
可能是垃圾回收问题。
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
File.Delete(picturePath);
回答by Eduardo Sobrinho
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!
回答by Anup Sharma
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=OneWayalong with GC.Collect()before calling File.Delete(path)and it worked finally.
我有类似的问题。唯一的区别是我使用的是绑定(MVVM 模式)。没什么工作,然后我删除了一切,用结合试着Mode=OneWay沿GC.Collect()调用之前File.Delete(path)最后和它的工作。
回答by JeffS
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.
我注释掉了那行,它得到了解决。

