3

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

lukemols
  • 71
  • 2
  • 11
  • Have you read these articles? https://msdn.microsoft.com/en-us/windows/uwp/files/quickstart-managing-folders-in-the-music-pictures-and-videos-libraries .... https://msdn.microsoft.com/en-us/windows/uwp/files/file-access-permissions – JuP Jan 26 '17 at 09:25
  • I think I've missed them. Thank you so much – lukemols Jan 27 '17 at 09:40

2 Answers2

5

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
}
Romasz
  • 29,662
  • 13
  • 79
  • 154
0

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.";
}
Michael
  • 11
  • 2
  • 3