Search code examples
c#uwpiosavestorage

How to save a file on any path of computer without dialog in c# uwp


I'm trying to save a file without a filepicker under my desktop or somewhere else, but I always get the error "Access to path [...] is denied"

This is my code:

    private async void SaveFile(string Path)
    {
        StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(Path);
        StorageFile sampleFile = await folder.CreateFileAsync("sample.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);
    }

I tried the same code but with the Localfoder and it worked.

Is there any way to get access to the path without a filepicker?


Solution

  • Using FilePicker gets permission from the user to access file locations. File system access is a restricted capability in UWP. You can enable Broad Filesystem Access in your package manifest to get this permission when the app is installed. It's not recommended for apps published to the Microsoft Store, because it makes it less likely for you app to be approved. See the App capability declarations documentation.

    Alternatively, you could run your app from a folder that contains the desktop in one of its subfolders by using the AppExecutionAlias extension mention under Accessing additional locations in File Access Permissions. Underneath that section there is also an explanation of Broad Filesystem Access as well.