访问路径被拒绝(Xamarin/Android) [英] Access to path denied (Xamarin/Android)

查看:57
本文介绍了访问路径被拒绝(Xamarin/Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am running Windows 10, Visual Studio 2015, and Xamarin. I am rather new to Xamarin (just to set the ground level). I am currently having an issue after I updated recently. My application was working before the update. All my files were read-only, and I had no issues prior the update. Along with that, I have rebuilt the project and I the "clean and rebuild" for the project as well. I have tried it with multiple apps I have, and the other applications do not have this issue. The issue I am getting the following error below.

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 搜索,因为我阅读了顶部结果中的几乎所有内容).我尝试以管理员身份运行该程序,并且该目录对任何人都具有开放访问权限.

I've read few articles such as (I posted the Google search for the 3rd item because I read almost everything in the top results). I've tried running the program as an administrator and the directory has open access to anyone.

对于任何对我如何访问我的文件的代码感到好奇的人,它在下面.

For anyone curious on the code of how I am accessing my file, it is below.

 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/

You should enable READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions in Android. You can follow this blog to enable the permission in runtime https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/

这篇关于访问路径被拒绝(Xamarin/Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆