0

I'm trying to write a file to

Environment.GetFolderPath(Environment.SpecialFolder.Personal)

I'm getting the following error when I try to do so:

System.UnauthorizedAccessException: Access to the path is denied

I suspect I have a more general problem.

To debug what might be going wrong here, I would like to try writing to a fail-safe directory (where I wouldn't need any permissions).

Could anybody tell me which directory I could choose for this?

If this also doesn't work, I'd know I have a more general problem. If this does work, it's likely that it's a permission problem.

Thank you.

tmighty
  • 10,734
  • 21
  • 104
  • 218

1 Answers1

0

maybe this help

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

Depending on the version of Android you are using even with the permissions added in the manifest in 6.0 or up the user has to explicitly enable the permission when the app runs and on lower versions permission is asked during install. For example, on startup of the app I created a method to check if it is enabled and request permission if it's not.

 private void CheckAppPermissions()
{
  if ((int)Build.VERSION.SdkInt < 23)
  {
    return;
  }
  else
  {
    if (PackageManager.CheckPermission(Manifest.Permission.ReadExternalStorage, PackageName) != Permission.Granted
        && PackageManager.CheckPermission(Manifest.Permission.WriteExternalStorage, PackageName) != Permission.Granted)
    {
        var permissions = new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage };
        RequestPermissions(permissions, 1);
    }
 }
}

You can also use the support library to do this, which is simpler and allows you to not have to check the Android version. For more info check out google's documentation.

BiRjU
  • 733
  • 6
  • 23
  • Thank you. Can you tell me where "RequestPermissions" and "PackageName" are located? I tried to simply paste your code into my cs file, all worked fine, but these 2 were not to be found in any library. – tmighty Apr 26 '18 at 07:44
  • RequestPermissions and PackageName both are in AndroidManifest.xml – BiRjU Apr 26 '18 at 07:47
  • Damn, I fell in love with this girl: https://developer.android.com/training/permissions/requesting – tmighty Apr 26 '18 at 16:59
  • please replace the `&&` in the `if()` with a `||` – thomiel Aug 28 '18 at 14:31