WPF无法删除文件

时间:2013-05-13 11:47:31

标签: c# wpf

我有以下代码:

imgScreenshot.Source = new BitmapImage(new Uri(ShellFolder.DocumentsFolder() + System.IO.Path.DirectorySeparatorChar + screenshot.Filename));
File.Delete(ShellFolder.DocumentsFolder() + System.IO.Path.DirectorySeparatorChar + screenshot.Filename);

我收到错误:

  

{"进程无法访问文件' C:\ Users \ rover \ Documents \ MagicScreenshot \ vEhWg3Ra20M.jpg'因为它正被另一个进程使用。"}

我考虑过处理BitmapImage,但是这个类没有实现这个接口。如何正确编写此代码?

1 个答案:

答案 0 :(得分:2)

尝试:

        BitmapImage bi = new BitmapImage();
        bi.BeginInit();
        bi.UriSource = new Uri(ShellFolder.DocumentsFolder() + System.IO.Path.DirectorySeparatorChar + screenshot.Filename);
        bi.CacheOption = BitmapCacheOption.OnLoad;
        bi.EndInit();
        imgScreenshot.Source = bi;
        File.Delete(ShellFolder.DocumentsFolder() + System.IO.Path.DirectorySeparatorChar + screenshot.Filename);