gpt4 book ai didi

c# - 访问路径被拒绝 (Xamarin/Android)

转载 作者:太空宇宙 更新时间:2023-11-03 22:57:21 25 4
gpt4 key购买 nike

我正在运行 Windows 10、Visual Studio 2015 和 Xamarin。我对 Xamarin 比较陌生(只是为了设置地面水平)。我最近更新后目前遇到问题。我的应用程序在更新之前工作。我所有的文件都是只读的,在更新之前我没有遇到任何问题。与此同时,我重建了该项目,并且还为该项目进行了“清理和重建”。我已经用我拥有的多个应用程序尝试过,其他应用程序没有这个问题。我在下面收到以下错误的问题。

06-26 13:51:55.108 I/MonoDroid( 6985): UNHANDLED EXCEPTION:
06-26 13:51:55.142 I/MonoDroid( 6985): System.UnauthorizedAccessException: Access to the path "/storage/emulated/0/defaultDirectory/users.ini" is denied.
06-26 13:51:55.142 I/MonoDroid( 6985): at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001f1] in <3fd174ff54b146228c505f23cf75ce71>:0
06-26 13:51:55.142 I/MonoDroid( 6985): at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options, System.String msgPath, System.Boolean bFromProxy, System.Boolean useLongPath, System.Boolean checkHost) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
06-26 13:51:55.142 I/MonoDroid( 6985): at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions,string,bool,bool,bool)
06-26 13:51:55.142 I/MonoDroid( 6985): at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks, System.Int32 bufferSize, System.Boolean checkHost) [0x00067] in <3fd174ff54b146228c505f23cf75ce71>:0
06-26 13:51:55.142 I/MonoDroid( 6985): at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks, System.Int32 bufferSize) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
06-26 13:51:55.142 I/MonoDroid( 6985): at System.IO.StreamReader..ctor (System.String path, System.Boolean detectEncodingFromByteOrderMarks) [0x0000d] in <3fd174ff54b146228c505f23cf75ce71>:0
06-26 13:51:55.142 I/MonoDroid( 6985): at System.IO.StreamReader..ctor (System.String path) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
06-26 13:51:55.142 I/MonoDroid( 6985): at (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string)
06-26 13:51:55.142 I/MonoDroid( 6985): at System.IO.File.OpenText (System.String path) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
06-26 13:51:55.142 I/MonoDroid( 6985): at QykAndroidApp.AdminLoginActivity.decryptUsers () [0x00033] in <65a7af1659a443738d96e6c2b7534ab2>:0
06-26 13:51:55.142 I/MonoDroid( 6985): at QykAndroidApp.AdminLoginActivity.OnCreate (Android.OS.Bundle savedInstanceState) [0x0008a] in <65a7af1659a443738d96e6c2b7534ab2>:0
06-26 13:51:55.142 I/MonoDroid( 6985): at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <d855bac285f44dda8a0d8510b679b1e2>:0
06-26 13:51:55.142 I/MonoDroid( 6985): at (wrapper dynamic-method) System.Object:28564880-3429-412d-9c61-4f5bb9fc103e (intptr,intptr,intptr)
06-26 13:51:55.153 W/art ( 6985): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
An unhandled exception occured.

我读过几篇文章,例如(我发布了对第 3 项的 Google 搜索,因为我几乎阅读了顶部结果中的所有内容)。我试过以管理员身份运行该程序,该目录对任何人都具有开放访问权限。

对于任何对我如何访问我的文件的代码感兴趣的人,它在下面。

 private List<string> readUsers()
{
adminUsers = new List<string>();
try
{
StreamReader readerForFile;

//create checks for making sure the card is mounted, the directory is found, and the file is found.
var filePath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "defaultDirectory/users.ini");

File.SetAttributes(filePath, FileAttributes.Normal);

if (File.Exists(filePath))
{
//Reads enttire document
//var fillContent = File.ReadAllText(filePath);
readerForFile = File.OpenText(filePath);
if (readerForFile == null) { return null; }
else
{
string line = "";
int counter = 0;
while ((line = readerForFile.ReadLine()) != null)
{
adminUsers.Add(line);
counter++;
}
}
}

}
catch (IOException e)
{
//You'll need to add proper error handling here
alert.SetMessage("File was not found. " + e).SetNeutralButton("Okay", delegate { QuestionActivity.exit(); }).Create().Show();
}

return adminUsers;
}

最佳答案

您应该在 Android 中启用 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 权限。您可以关注此博客以在运行时启用权限 https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/

关于c# - 访问路径被拒绝 (Xamarin/Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44762048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com