【问题标题】:System.UnauthorizedAccessException Denied ErrorSystem.UnauthorizedAccessException 拒绝错误
【发布时间】:2019-11-14 00:00:37
【问题描述】:

当我用我的应用程序启动我的模拟器时,我得到了UnauthorizedAccessException,它告诉我“Access to /storage/emulated/0/Download/MenuPlayer is denied”。

这仅在我尝试创建文件或打开文件夹以将文件下载到其中时发生。

我的 AndroidManifest.xml 代码是:

<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" />

我试图访问它的代码是:

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");

错误是:

未处理的异常:

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

【问题讨论】:

    标签: c# android xamarin emulation


    【解决方案1】:

    即使您在清单中启用了 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 权限,您也可能需要在代码中明确地做一些事情。你已经这样做了吗?

    详情见下文

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

    希望对你有帮助

    【讨论】:

    • 感谢 user007 的回复,我设法解决了,显然我所要做的就是在弹出错误后继续,然后在模拟器上启用存储权限。
    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2015-02-28
    • 2017-01-24
    相关资源
    最近更新 更多