Encountering Access is denied error in UWP app when attempting to create file/folders on C:\ drive

Sharath HG 0 Reputation points
2024-01-10T06:05:43.1766667+00:00

GitHub link for the project

I am encountering the following error while attempting to create folders and read/write files directly on the C:\ drive for a UWP app:

Access is denied. Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)

I have already granted the broadFileSystemAccess capability to the project and have enabled file access in both the file system settings and by right-clicking on the app and selecting app settings. Despite these configurations, the issue persists.

Can someone please provide guidance on how to resolve permissions issues and explore alternative methods to enable direct folder creation without relying on user selection? Any insights or solutions would be greatly appreciated!

private async void myBtn_Click(object sender, RoutedEventArgs e)
{
    // Specify the folder path
    string folderPath = @"C:\\RGNEW";
    
    // Get the StorageFolder from the path
    StorageFolder folder = StorageFolder.GetFolderFromPathAsync(folderPath).AsTask().Result;
    
    // Add the folder to FutureAccessList with the folder path as the token
    string token = folderPath; // You can customize the token if needed
    StorageApplicationPermissions.FutureAccessList.AddOrReplace(token, folder);
}
Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 34,101 Reputation points Microsoft External Staff
    2024-01-10T07:12:46.6133333+00:00

    Update:

    I want to create a folder in C drive from my UWP app. I modified your code a little bit. The following code creates a folder named AAA in C driver and add it to the future access list with token AAA. You could try it.

       string folderPath = @"C:";
    
       // Get the StorageFolder from the path
        StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(folderPath);
        var targetfolder=  await folder.CreateFolderAsync("AAA");
    
       // Add the folder to FutureAccessList with the folder path as the token
       string token = "234"; // You can customize the token if needed
       StorageApplicationPermissions.FutureAccessList.AddOrReplace(token, targetfolder);
    

    Old:

    I created a test folder in C first and then created a blank UWP project to test your code. I got different behaviors with the code. Enabled the capability both in the project and system setting.

    Frist is that the folderPath is not correct. It should be @"C:\RGNEW". After changing the path. GetFolderFromPathAsync works correctly.

    Second, FutureAccessList.AddOrReplace will give a parameter error if you use path as token. Using RGNEW as token works.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.