Home > Net >  File.WriteAllText System.UnauthorizedAccessException Access to the path 'c:\mafirst.json'
File.WriteAllText System.UnauthorizedAccessException Access to the path 'c:\mafirst.json'

Time:03-31

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.

CodePudding user response:

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):

enter image description here

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

  • Related