我正在尝试使用以下方法删除图像: (文件路径100%正确)
if(File.Exists(filePath))
File.Delete(filePath);
我收到以下异常:
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.
最佳答案
对于直接绑定(bind)到 Image 控件的 ImageSource 的图像来说,这是一个常见问题。您应该通过 BitmapImage 创建 ImageSource 并将 BitmapImage.CacheOption 属性设置为 BitmapCacheOption.OnLoad:
BitmapImage bi = new BitmapImage();
// Begin initialization.
bi.BeginInit();
// Set properties.
bi.CacheOption = BitmapCacheOption.OnLoad;
//
bi.EndInit();
关于wpf - 在运行时删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4498769/