Reputation: 18279
I've got a Universal Windows Platform app that saves files. For the most part it works perfectly. However, whenever I attempt to save a file into a folder watched by Dropbox, I'm getting this exception:
Platform::AccessDeniedException ^ at memory location 0x045FD0D0. HRESULT:0x80070005 Access is denied. WinRT information: Access is denied.
This is caused by this line in my code, called right before I save the file:
CachedFileManager::DeferUpdates(file);
Nothing in the documentation for this call suggests to me what the problem is, or indeed that this exception could even be thrown, though of course exceptions aren't well-documented throughout most of the API.
This problem does not occur in folders watched by OneDrive.
It does not appear to matter whether or not the file is already synced to Dropbox. If the file is new, then a new file will be created and then this exception will be thrown. If the file is existing, then this exception will be thrown before it can be updated.
Why is this happening and how can I properly fix it?
Right now, I've simply enclosed the line in a try catch block where I discard AccessDeniedException
.
Update: I just switched to Windows Insider previews. I'm on Build 14295 and this problem has slightly mutated. This is the new exception:
Platform::COMException ^ at memory location 0x00000043892FB9F0. HRESULT:0x8000FFFF Catastrophic failure
Still have no idea why this is happening, but a similar hacked solution works, where now I also discard COMException
.
Upvotes: 1
Views: 932
Reputation: 680
This means you don't have access to save files at that location from your Winrt code. It is locked down pretty tight as far as file IO permissions go. It sounds like you are not using FileSavePicker to save the file but rather you are creating the filestorage object yourself and specifying the file path location manually?
If you use FileSavePicker, it will return a stroagefile object that has all the permissions you need to save the file at whatever location the user selected.
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add(this.loadedFile.FileType.Substring(1), new List<string>() { this.loadedFile.FileType });
// Default extension if the user does not select a choice explicitly from the dropdown
savePicker.DefaultFileExtension = this.loadedFile.FileType;
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = file.Name.Trim();
//savePicker.view
StorageFile savedItem = await savePicker.PickSaveFileAsync();
if (savedItem != null)
{
CachedFileManager.DeferUpdates(savedItem);
await FileIO.WriteBytesAsync(savedItem, new byte[0]); //truncate current content in case it already exists
await FileIO.WriteBytesAsync(savedItem, buffer);
await CachedFileManager.CompleteUpdatesAsync(savedItem);
}
Upvotes: 0