C# 如何正确删除存储文件?

C# 如何正确删除存储文件?,c#,windows-store-apps,C#,Windows Store Apps,在我的Windows应用商店应用程序中,我的存储文件夹中有一些文件(图像) 然后,当我想使用图像时,我创建如下位图: BitmapImage BitmapImage = new BitmapImage(new Uri("full_file_path")); 而我的Image控件具有ImageSource=“{Binding Path=BitmapImage}” 一切都很好。但当我想删除我的文件时,我使用: BitmapImage = null; 然后: try { a

在我的Windows应用商店应用程序中,我的存储文件夹中有一些文件(图像)

然后,当我想使用图像时,我创建如下位图:

BitmapImage BitmapImage = new BitmapImage(new Uri("full_file_path"));
而我的
Image
控件具有
ImageSource=“{Binding Path=BitmapImage}”

一切都很好。但当我想删除我的文件时,我使用:

BitmapImage = null;
然后:

  try
  {
        await storageFile.DeleteAsync(StorageDeleteOption.Default);
  }
  catch (UnauthorizedAccessException e)
  {
        // and I get exception here.
  }

问题:我应该如何正确地删除文件?

听起来好像您正试图删除加载的图像文件。在释放图像之前,文件将保持锁定状态。在我看来,我认为您应该在加载图像后复制它,并在图像控件中使用该图像副本

您可以使用以下代码为您的场景创建一个新的应用程序,而无需在删除之前处理图像

BitmapImage bmpImage = new BitmapImage();
bmpImage.BeginInit();
Uri uri = new Uri(filePath,UriKind.RelativeOrAbsolute);
bmpImage.UriSource = uri;
bmpImage.CacheOption = BitmapCacheOption.OnLoad;
bmpImage.EndInit();

看看是否有帮助。:)

谢谢,但我认为windows应用商店不支持System.windows.Media.Imaging(使用windows.UI.Xaml.Media.Imaging;)