2013-05-13 189 views
2

我尝试通过C#读取存在于我的平板电脑/mnt/sdcard/位置的.txt文件。每当我执行程序中,我看到下面错误味精由于C#中的访问被拒绝问题而无法读取文件

访问路径/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. 

     } 
    } 
} 

如果任何一个人都来acrossed这个问题,并找到解决办法,请分享...

感谢 迪帕克

+0

你用什么框架?为什么不使用Java SDK? – Gabor 2013-05-13 12:08:12

+0

您的存储装置已安装?如果是这样,您将无法访问它,直到它被卸载。 – Guardanis 2013-05-13 13:46:01

回答

0

随着代码下面几行,我就阅读txt文件目前我Android平板电脑

string path = 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)); 

       } 

      } 

     }