Forum Xamarin.Android

System.UnauthorizedAccessException Access to the path

Hello,
I have a problem with access rights. The problem has often been encountered in different forums.
I want to know why in UWP and Xamarin Android we had difficulty writing in an external folder?
When I plug in a USB stick I detect the pat. Here => /storage/XXX for Android and F:/XXX for UWP.
The problem is when I use a file creation function, I have an exception.

System.UnauthorizedAccessException : Access to the path “/storage/XXX/PocSav/XX.txt is denied.

At System.IO.FileStream etc….

I don’t know what is the problem.

public void WriteLocalFile(string FileName, string Data)
        {
            string filePath = Path.Combine((string)UsbPicker.SelectedItem + "/PocSave", FileName);
            Directory.CreateDirectory((string)UsbPicker.SelectedItem + "/PocSave");
            File.WriteAllText(filePath, Data);
        }
private void SaveButton_Clicked(object sender, EventArgs e)
        {
            try
            {
                WriteLocalFile(TextTxtEntry.Text + ".txt", CommentaryEditor.Text);
            }
            catch (Exception ex) when (ex.ToString() != null)
            {
                DisplayAlert(ex.Source, ex.ToString(), "OK");
            }
        }

For the permission:

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
        <uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" />

Can someone guide me to the beginning of a solution?
Used[assembly] ?

Thank for the help.
Zebiphire

Posts

  • Zebiphire67Zebiphire67 Member ✭✭

    Hello again,

    Now with the new lvl sdk I will check for the number version directly in the MainActivity from Android.

     if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, 0);
                }
    
                if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage }, 0);
                }
    

    I can access to the .txt file and read it with File.ReadAllText(filePath); but I can't write on it.
    System.UnauthorizedAccessException Access to the path all the time. I don't know why I can't have access to it.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I have now the same problem with an application that has always worked... have you found a solution?

  • MarcoSalvatoriMarcoSalvatori USMember ✭✭✭

    @Zebiphire67 said:
    Hello again,

    Now with the new lvl sdk I will check for the number version directly in the MainActivity from Android.

     if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage) != (int)Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, 0);
                }
    
                if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage }, 0);
                }
    

    I can access to the .txt file and read it with File.ReadAllText(filePath); but I can't write on it.
    System.UnauthorizedAccessException Access to the path all the time. I don't know why I can't have access to it.

    today I had the same problem.
    it was enough for me to add the instructions written by Zebiphire67.
    if you need you can ask ...

Sign In or Register to comment.