'File.WriteAllText System.UnauthorizedAccessException Access to the path 'c:\mafirst.json' is denied error in Xamarin.forms using JSON
I'm new to xamarin and i want to write some data to a json file from a simple button_clicked event.
Here's my code: `
JObject jsontry = new JObject(
new JProperty("newprop", "true"));
File.WriteAllText(@"c:\mafirst.json", jsontry.ToString());//this line gives the error
// write JSON directly to a file
using (StreamWriter file = File.CreateText(@"c:\Users\Flora\Desktop\mafirst.json"))//this line gives the same error as well
using (JsonTextWriter writer = new JsonTextWriter(file))
{
jsontry.WriteTo(writer);
}
`
Error: System.UnauthorizedAccessException: 'Access to the path 'c:\mafirst.json' is denied.'
I tried different paths, writing same code in the same project but different .cs files, basically i have tried every idea that comes to my mind and the solutions from the net but nothing works.
Edit:
Sorry for the lack of information about the app. This app is just a learning project for me. I run this code in just the UWP app so i treated it like a UWP app which is logical i guess(?) and thats the only reason that i use a path like (C:\Desktop\blabla.bla).
I run the visual studio with admin privileges and that doesn't seem to work, i just want to write and store some data from my UWP app's C# codes to a JSON file in the local.
Thanks for help.
Solution 1:[1]
Xamarin apps run in a “sandbox”: apps can access very little unless they have the rights and they can only write to and read in folders that are created for your app (so that apps cannot mess with the files/data of other apps).
For file storage inside the sandbox of your app check out (for example):
https://docs.microsoft.com/en-us/xamarin/android/platform/files/
You can write files inside the folder of which the path is given by (for example):
System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData)
The path that is returned is different on Windows, Android and iOS. The most important thing: you can acces it.
Note that there are several Environment.SpecialFolder enum values you can choose.
For a file path, you could for example use:
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "somefile.txt");
I have used Environment.SpecialFolder.LocalApplicationData a lot in a shared project for UWP and Android.
Solution 2:[2]
At first, the path you used c:\Users\Flora\Desktop\mafirst.json is on your PC.
When you use the Xamarin.Forms, you could use this path for UWP. But for the Android and ios, you could not.
On UWP, when you want to read or write file on Desktop, you could to set the capabilities.
Add the broadFileSystemAccess like below in Package.appxmanifest of UWP.
<Package
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:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
.............
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
</Package>
And then deploy the app to turn on the File system permission in App Settings.
Use the StorageFolder to get the folder and create file if file is not exist. And it need dependency service to complete it.
Create interface in Xamarin.Forms:
public interface IFileManager
{
void WriteFile();
}
Implementation in UWP:
[assembly: Dependency(typeof(FileManager))]
namespace XFUWPFileSystem.UWP
{
public class FileManager : IFileManager
{
public async void WriteFile()
{
JObject jsontry = new JObject(
new JProperty("newprop", "true"));
var folder = await StorageFolder.GetFolderFromPathAsync(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));//Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData
StorageFile sampleFile = await folder.CreateFileAsync("mafirst.json", CreationCollisionOption.OpenIfExists);
await Windows.Storage.FileIO.WriteTextAsync(sampleFile, jsontry.ToString());
}
}
}
You could check the link below about the Locations that UWP apps can access: https://docs.microsoft.com/en-us/windows/uwp/files/file-access-permissions
Dependency service for UWP: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction#universal-windows-platform
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Wendy Zang - MSFT |

