我有一个从选择器获得的图像文件。我将其保存为“me.png”:
Windows.Storage.ApplicationData.Current.LocalFolder
如果我再次执行该操作以尝试用新文件覆盖旧文件,应用程序将崩溃:
An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code
Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
If there is a handler for this exception, the program may be safely continued.
我用来覆盖文件的代码是:
await file.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder, "me.png", Windows.Storage.NameCollisionOption.ReplaceExisting);
知道为什么会这样吗?它第二次在该行代码上崩溃。如果我将冲突选项更改为 GenerateUniqueName 它可以正常工作,因此覆盖文件似乎是一个问题。
更新:
完整代码
localSettings.Values["me_image_path"] = "Asssets/pinkBackground.png";
tile.BackgroundImage = localSettings.Values["me_image_path"];
var picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.ViewMode = PickerViewMode.Thumbnail;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
StorageFile file = await picker.PickSingleFileAsync();
if (file == null) return;
await file.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder, "me.png", Windows.Storage.NameCollisionOption.ReplaceExisting);
localSettings.Values["me_image_path"] = "ms-appdata:///local/me.png";
tile.BackgroundImage = localSettings.Values["me_image_path"];