UWP App Keeps Throwing Unauthorized Access Exception


I am making an UWP App and I keep having a System.UnauthorizedAccessException

Here is the full Code of my main file

private void SaveButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
            var user = new Configuration();
            string Email = txtEmail.Text.Trim();

                user[Email]["Value"].StringValue = "test";

            string dest = Path.GetFullPath(@"C:\Users\IT\Desktop\AttendanceApp\AttendanceApp\bin\x86\Debug\AppX\Test\user.cfg");

As I was reading the documentation for File Access Permission for UWP apps, it had suggested to add this line

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp uap5 rescap"> ...

And this is my AppxManifest file

<Package xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp uap5 rescap" xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5">
    <rescap:Capability Name="broadFileSystemAccess" />

So when I try to save a file. I still keep having this error.

System.UnauthorizedAccessException HResult=0x80070005
Message=Access to the path 'C:\Users\IT\Desktop\AttendanceApp\AttendanceApp\bin\x86\Debug\AppX\Test\user.cfg' is denied. Source=System.Private.CoreLib StackTrace: at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) at SharpConfig.Configuration.SaveToFile(String filename, Encoding encoding) at SharpConfig.Configuration.SaveToFile(String filename) at AttendanceApp.MainPage.SaveButton_Click(Object sender, RoutedEventArgs e) in C:\Users\IT\Desktop\AttendanceApp\AttendanceApp\MainPage.xaml.cs:line 57

By the way I am also using SharpConfig to edit my .cfg files

Any ideas why this happens? Any help would be great

asked on Stack Overflow Jan 18, 2019 by newboooooo

1 Answer


There are two places you need to be aware of:

  1. As @mm8 said, the path in your above code C:\Users\IT\Desktop\AttendanceApp\AttendanceApp\bin\x86\Debug\AppX\ equals Windows.ApplicationModel.Package.Current.InstalledLocation.Path. In UWP, the installed localtion folder is read-only. You cannot use any APIs to write in it. You could think about coping 'user.cfg' to other place, then you could write to it. The ApplicationData folder would be a good choice. For example, LocalFolder.
  2. I saw that you add the broadFileSystemAccess capability to access files outside the app cantainer. That's great, but you missed an important prompt 'This capability works for APIs in the Windows.Storage namespace.'. I've checked the SharpConfig's source code, the SaveToFile method uses the 'FileStream' relevant APIs to write file. It's not included in 'Windows.Storage Namespace'. So, the 'broadFileSystemAccess' capanility doesn't work for it.

So, if you have to use 'SharpConfig' in your project, you need to use the 'Windows.Storage Namespace' APIs to change its source code and compile a custom version for your UWP project. In the meantime, keep in mind my explanation in the first paragraph.

answered on Stack Overflow Jan 21, 2019 by Xavier Xie • edited Jan 21, 2019 by Xavier Xie

User contributions licensed under CC BY-SA 3.0