I'm trying to make a UWP app which can export a file saved in his own storage into the document library.
In Package.appxmanifest I've inserted the following lines:
<uap:Capability Name="picturesLibrary" />
<uap:Capability Name="documentsLibrary" />
The code to get the path is this:
StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null /* current user */, KnownFolderId.DocumentsLibrary);
string path = storageFolder.Path + "\\" + fileName;
The code to save the file is this:
FileStream writer = new FileStream(filePath, FileMode.Create);
At this point, the program launches this exception:
Access to the path 'C:\Users\luca9\AppData\Roaming\Microsoft\Windows\Libraries\Documents.library-ms' is denied.
On my 950XL, the exception is similar:
Access to the path 'C:\Data\Users\DefApps\APPDATA\ROAMING\MICROSOFT\WINDOWS\Libraries\Documents.library-ms' is denied.
I've tryied both on Documents and Pictures libraries, but I get the same exception.
How can I solve it?
Thank you in advance,
Luca
Don't get FileStream with path - the app doesn't have privileges. As you already have StorageFolder , use it to create a StorageFile and then get stream from it with one of its methods, for example:
var file = await storageFolder.CreateFileAsync("fileName");
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
// do what you want
}
this example from Microsoft works with uwp.
https://learn.microsoft.com/en-us/windows/uwp/files/quickstart-save-a-file-with-a-picker
1. Create and customize the FileSavePicker
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = "New Document";
2. Show the FileSavePicker and save to the picked file
Windows.Storage.StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
// Prevent updates to the remote version of the file until
// we finish making changes and call CompleteUpdatesAsync.
Windows.Storage.CachedFileManager.DeferUpdates(file);
// write to file
await Windows.Storage.FileIO.WriteTextAsync(file, file.Name);
// Let Windows know that we're finished changing the file so
// the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
Windows.Storage.Provider.FileUpdateStatus status =
await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);
if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
{
this.textBlock.Text = "File " + file.Name + " was saved.";
}
else
{
this.textBlock.Text = "File " + file.Name + " couldn't be saved.";
}
}
else
{
this.textBlock.Text = "Operation cancelled.";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.