Xamarin : Android : System.UnauthorizedAccessException: Access to the path is denied

前端 未结 5 1785
臣服心动
臣服心动 2020-12-06 16:52

So I\'m trying to create a file and I\'m getting System.UnauthorizedAccessException: Access to the path \"/DownloadJitters\" is denied. I\'m not sure if it\'s a permissions

相关标签:
5条回答
  • 2020-12-06 17:16

    If you are still getting UnauthorizedAccessException for write or read file in Xamarin Android. I just written article to resolve it http://bsubramanyamraju.blogspot.com/2019/12/resolved-unauthorizedaccessexception.html

    0 讨论(0)
  • 2020-12-06 17:22

    First of all add this permissions to you Manifest:

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

    Since Android 6.0 (API 23) you need also to request the permissions manually, add this code on your MainActivity.cs on your Xamarin.Android project:

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

    Since Android 10 you may also need to add android:requestLegacyExternalStorage attribute to your Manifest like this:

    <application android:requestLegacyExternalStorage="true" />
    
    0 讨论(0)
  • 2020-12-06 17:24

    Xamarin.Forms (Android solution)

    MainActivity.cs

    • For apps that target Android 5.1(API level 22) or lower, there is nothing more that needs to be done.
    • Apps that will run on Android 6.0(API 23 level 23) or higher should ask Run time permission checks.
    protected override void OnCreate(Bundle bundle)
    {
        if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
        {
            if (!(CheckPermissionGranted(Manifest.Permission.ReadExternalStorage) && !CheckPermissionGranted(Manifest.Permission.WriteExternalStorage)))
            {
                RequestPermission();
            }
        }
        LoadApplication(new App());
    }
    
    private void RequestPermission()
    {
        ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage }, 0);
    }
    
    public bool CheckPermissionGranted(string Permissions)
    {
        // Check if the permission is already available.
        if (ActivityCompat.CheckSelfPermission(this, Permissions) != Permission.Granted)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    0 讨论(0)
  • 2020-12-06 17:26

    Ok I Fixed it by changing the saving location to System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

    Don't ask me why that worked when they need the same permissions but it did.

    0 讨论(0)
  • 2020-12-06 17:28

    This looks like a copy and paste error - you should learn to refactor common code and expressions into one value and reuse it.

    //Checks Directory exists
    if (File.Exists(Android.OS.Environment.DirectoryDownloads + "/Jitters/FavouritesListAdded.txt") == false)
    {
        Directory.CreateDirectory(Android.OS.Environment.DirectoryDownloads + "Jitters/FavouriteList/");
        File.Create(Android.OS.Environment.DirectoryDownloads + "/Jitters/FavouritesListAdded.txt");
    }
    

    Let's assume Android.OS.Environment.DirectoryDownloads has the value /Downloads. Now go through the code line by line (you should really do this with a debugger):

    File.Exists(Android.OS.Environment.DirectoryDownloads + "/Jitters/FavouritesListAdded.txt")
    

    The parameter value here will be "/Downloads/Jitters/FavouritesListAdded.txt" - OK

    Directory.CreateDirectory(Android.OS.Environment.DirectoryDownloads + "Jitters/FavouriteList/");
    

    There's no leading slash on the literal string here, so the value will be: /DownloadsJitters/FavouriteList - I'm guessing you probably meant it to be /Downloads/Jitters/FavouriteList.

    Rather than making sure slashes are added to all 6 path expressions in your code - just create one variable with the path value and reuse it.

    0 讨论(0)
提交回复
热议问题