UnauthorizedAccessException - DeleteAsync StorageFile

时间:2013-12-17 15:54:24

标签: c# windows-runtime

我将文件存储在ObservableCollection中(FileItem包含StorageFile文件)。在GridView中,我绑定到此集合,并显示处理ContainerContentChaning事件的图像。

     private async void GridView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
    {
        var item = args.Item as IImageProvider;
        var imageParent = args.ItemContainer.ContentTemplateRoot as Border;
        var imageControl = imageParent.Child as Image;
        ...
        imageControl.Source = await item.GetImage(ImageSizeType.Thumbnail);
    }

我也有删除文件的命令 - 就像这样:

     images.Remove(item);
     await item.PerformStorageFileOperation(async (file) =>
     {
          await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
     }, System.Threading.CancellationToken.None);

其中item是FileItem,而PerformStorageFileOperation看起来像这样:

  public async Task PerformStorageFileOperation(Func<StorageFile, Task> operation, CancellationToken cancelToken)
{
    await _syncLock.WaitAsync(cancelToken);
    await operation(File);
    _syncLock.Release();
}

SyncLock是SemphoreSlim。 .GetImage和删除文件函数都使用PerfomStorageFileOperation,所以我猜两种方法都不可能同时使用StorageFile。 GetImage从byte []获取Image - 没有设置/绑定BitmapImage源到任何文件流。偶尔我会得到UnathorizedAccessException - 但我怀疑它是由文件特权不足和类似的事情造成的,因为我在删除时可以获得3次异常,并且说第四次正确删除相同的文件。

0 个答案:

没有答案
相关问题