0

I'm trying to write a list to a txt file via the following code with my UWP app:

string filePath = @"C:\receipt.txt"; File.WriteAllLines(filePath, output);

but I get the following error: System.UnauthorizedAccessException: 'Access to the path 'C:\receipt.txt' is denied.'

I've run VS as admin and added an app manifest and adjusted but still no luck.

Any help would be greatly appreciated.

Edit: I added the broadFileSystemAccess to the program and allowed it permissions through windows settings but still get the same error. Also I changed it to @"C:\Receipts\receipt.txt"; but still no luck. Works fine with framework console app but I think that's because UWP doesn't have same access (at least from what I read online)

  • 1
    Does this answer your question? [UWP access denied](https://stackoverflow.com/questions/47621720/uwp-access-denied) – Lee Taylor Apr 28 '22 at 17:57
  • Why not add the file picker to allow the user to specify where the file is? That should solve the problem. Hardcoding the path opens up opportunity for it not to be there....if it's a static file, you could always place it in the appdata folder. – Shawn Ramirez Apr 28 '22 at 18:02

1 Answers1

0

While not the best solution, fix for me was:

Change method to async:

Replace filepath string with

var file = await StorageFile.GetFileFromPathAsync("C:\\Receipts\\receipt.txt");

Replace WriteAllLines() with

await Windows.Storage.FileIO.WriteLinesAsync(file, output);
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77