2

我正在尝试/mnt/sdcard/通过 C# 读取我的平板电脑中存在的 .txt 文件。每当我执行程序时,我都会看到以下错误消息

拒绝访问路径 /mnt/sdcard/.android_secure。

在代码 Activity 中添加了权限,如下面的代码所示,但我仍然无法访问该文件。

经历了 n 个线程,但没有一个是清楚的。

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace Server

{
         [Activity (Label = "Server", MainLauncher = true, Permission = "android.permission.WRITE_EXTERNAL_STORAGE")]

    public class Activity1 : Activity
            {
        int count = 1;
        protected override void OnCreate (Bundle bundle)

        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource

            SetContentView (Resource.Layout.Main);
            Button button = FindViewById<Button> (Resource.Id.myButton);

            button.Click += delegate {
            button.Text = string.Format ("{0} clicks!", count++);};

    string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
string[] filePaths = Directory.GetFiles(path,"*.txt",SearchOption.AllDirectories); //In this line I see Access denied error in run time.

        }
    }
}

如果有人遇到此问题并找到解决方案,请分享...

谢谢迪帕克

4

1 回答 1

0

通过以下代码行,我将读取我的 Android 平板电脑上的 .txt 文件

字符串路径 = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.Path, "IVIFile.txt");

        using (FileStream fsSource  = new FileStream(path, FileMode.Open, FileAccess.Read))

        {

            byte[] bytes = new byte[fsSource.Length];

            int numBytesToRead = (int)fsSource.Length;



            while (numBytesToRead > 0)

            {

                byte[] b = new byte[numBytesToRead];

                UTF8Encoding temp = new UTF8Encoding(true);

                while (fsSource.Read(b,0,b.Length) > 0)

                {

                    Console.WriteLine(temp.GetString(b));

                }

            }

        }
于 2013-05-15T08:23:28.593 回答