我有一个来自选择器的图像文件。我把它保存为'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"];