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.
精彩评论