简体   繁体   中英

System.UnauthorizedAccessException Denied Error

I'm getting the UnauthorizedAccessException when starting up my emulator with my application, it tells me that " Access to /storage/emulated/0/Download/MenuPlayer is denied ".

This only occurs when I try to create a file or open up a folder to download files into it.

My AndroidManifest.xml code is:

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

My Code that is trying to access this is:

try {
    /// Get the path to the downloads folder
    var donwloadDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
    path = Path.Combine(donwloadDir.AbsolutePath, dirName);
    dirInfo = new DirectoryInfo(path);
    ///Create a path to the config file folder.
    configFilePath = Android.OS.Environment.GetExternalStoragePublicDirectory("").AbsolutePath + "/";
} catch(Exception e) {
    Android.Util.Log.Debug(TAG, "STUFF HERE..." + configFilePath);
    Android.Util.Log.Debug(TAG, e.Message);
    System.IO.File.WriteAllText(@"C:\logs.txt", e.Message);
}
System.IO.File.WriteAllText(@"C:\logs.txt", Android.OS.Environment.DirectoryDownloads);
Android.Util.Log.Debug(TAG, configFilePath);
Android.Util.Log.Debug(TAG, "END | PlayerReader");

the error is:

Unhandled Exception:

System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/Download/MenuPlayer" is denied. occurred

Even if you have enabled READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permission in manifest, you might need to do few things explicitly in code. Are you doing that already?

See post below for details

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

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM