开发者

Deleteing an image in run time

I am trying to delete an image with the method: (the file path is correct by 100%)

if(File.Exists(filePath))
   开发者_JS百科File.Delete(filePath);

and I am getting the following exception :

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: The process cannot access the file 'C:\visual_programming\yad2\yad2\bin\Debug\images\1.jpg' because it is being used by another process.


It's a common issue for the images that are binded directly to ImageSource of the Image control. You should create the ImageSource through BitmapImage and set BitmapImage.CacheOption property to BitmapCacheOption.OnLoad:

BitmapImage bi = new BitmapImage();

// Begin initialization.
bi.BeginInit();

// Set properties.
bi.CacheOption = BitmapCacheOption.OnLoad;
// 
bi.EndInit();

More Details about BitmapImage.CacheOption on MSDN


It's telling you exactly what's wrong; another process is currently accessing the file.

See here for some suggestions as to how you can identify that process.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜