Search code examples
c#uwpstreamwriter

Writing to file giving unauthorized access in c#


FileStream fs = new FileStream("C:\\Users\\admin\\Documents\\test1.rtf", FileMode.Open, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("Hello World");
sw.Dispose();
fs.Dispose();

This code is giving an unauthorized access error even though I declared the <rescap:Capability Name="broadFileSystemAccess"/> in the manifest and allowed the app those permissions in privacy setting in Windows 10. Any help is appreciated.


Solution

  • The problem is that you don't have used the Windows Storage namespace APIs. If you read the document File access permissions, you would know this point.

    This capability(broadFileSystemAccess) works for APIs in the Windows.Storage namespace.