4

我有以下代码:

Bitmap image = new Bitmap(filePath);
...
image.Save(someOtherPath);
image.Dispose();
File.Delete(filePath);
File.Move(someOtherPath, filePath);

File.Delete 行引发以下错误:

The process cannot access the file '...' because it is being used by another process.

如何让 C# 释放它对文件的锁定?

4

2 回答 2

3

尝试

using(var image = new Bitmap(filepath))
{
    image.Save(someOtherPath);
}


File.Delete(filePath);
File.Move(someOtherPath, filePath);
于 2012-08-29T08:47:49.900 回答
0
the mistake came from the file open by your Image class
Image image;

using(FileStream myStream = new FileStream(path))
  {    
     image = Image.FromStream(myStream);
  }
于 2012-08-29T08:55:38.240 回答