C# 我可以从txt文件内部存储器(documents文件夹)读写,但无法浏览到该位置,因为它没有';不存在
好吧,我在Xamarin Studio中用C#编程读/写文件。它在我的设备上工作 但是,当我将文件写入的确切路径输出到控制台时,该路径甚至不存在于整个手机中的任何位置 怎么样C# 我可以从txt文件内部存储器(documents文件夹)读写,但无法浏览到该位置,因为它没有';不存在,c#,android,xamarin,xamarin.android,xamarin-studio,C#,Android,Xamarin,Xamarin.android,Xamarin Studio,好吧,我在Xamarin Studio中用C#编程读/写文件。它在我的设备上工作 但是,当我将文件写入的确切路径输出到控制台时,该路径甚至不存在于整个手机中的任何位置 怎么样 using System; using System.IO; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; nam
using System;
using System.IO;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace ToolbarSample
{
[Activity(Label = "ToolbarSample", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
string content = "Jason rules";
string filename = "file.txt";
var documents = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.button);
TextView viewer = FindViewById<TextView>(Resource.Id.textView1);
if (File.Exists(documents + @"/" + filename))
{
string newContent = File.ReadAllText(documents + @"/" + filename);
if (viewer != null)
{
viewer.Text = newContent;
Console.WriteLine("File exists in: " + documents + @"/" + filename);
}
}
if (button != null)
{
button.Click += delegate
{
button.Enabled = false;
if (!Directory.Exists(documents))
{
viewer.Text = "Directory not found: " + documents;
}
else
{
Console.WriteLine("Directory exists.");
File.WriteAllText(documents + @"/" + filename, content);
if (!File.Exists(documents + @"/" + filename))
{
viewer.Text = "File not found: " + documents + @"/" + filename;
}
else
{
string newContent = File.ReadAllText(documents + @"/" + filename);
if (viewer != null)
{
viewer.Text = newContent;
Console.WriteLine("File exists in: " + documents + @"/" + filename);
}
}
}
};
}
}
}
}
使用系统;
使用System.IO;
使用Android.App;
使用Android.Content;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.OS;
命名空间工具栏示例
{
[活动(Label=“ToolbarSample”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
string content=“Jason规则”;
字符串filename=“file.txt”;
var documents=System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
//从布局资源中获取我们的按钮,
//并在其上附加一个事件
Button Button=FindViewById(Resource.Id.Button);
TextView viewer=FindViewById(Resource.Id.textView1);
如果(File.Exists(documents+/“+文件名))
{
字符串newContent=File.ReadAllText(文档+@“/”文件名);
如果(查看器!=null)
{
viewer.Text=newContent;
Console.WriteLine(“文件存在于:“+documents+@”/“+filename”);
}
}
如果(按钮!=null)
{
按钮。单击+=委派
{
按钮。已启用=错误;
如果(!Directory.Exists(documents))
{
viewer.Text=“未找到目录:”+文档;
}
其他的
{
WriteLine(“目录存在”);
File.writealText(文档+@“/”+文件名,内容);
如果(!File.Exists(documents+/“+filename))
{
viewer.Text=“未找到文件:“+文档+@”/”+文件名;
}
其他的
{
字符串newContent=File.ReadAllText(文档+@“/”文件名);
如果(查看器!=null)
{
viewer.Text=newContent;
Console.WriteLine(“文件存在于:“+documents+@”/“+filename”);
}
}
}
};
}
}
}
}
从内部SD卡成功读取后,将以下内容输出到控制台:
目录存在。文件存在于:
/data/data/ToolbarSample.ToolbarSample/files/file.txt
但是使用(许多不同的)文件管理器(都具有根访问权限)和显示的隐藏文件,我无法导航到该路径,因为它不存在。我甚至用整个手机搜索了“file.txt”,结果一个也没有出现。但是,每当我打开应用程序并单击按钮时,我都可以读取该文件。您指定位置的文件确实存在。您无法通过USB和文件资源管理器从PC访问该位置,但如果您使用良好的文件管理器应用程序(如根资源管理器),则可以访问该位置(和文件) 如果您真的希望用户能够访问这些保存的文件,我建议您将这些文件保存到更好的位置,以便用户可以轻松地通过USB将文件从手机传输到计算机 从文件中读取/写入数据非常简单
“这条路的哪一部分不存在?”西蒙说。甚至第一部分:数据/数据。整个手机上没有一个位置包含名为data的子文件夹,其父文件夹也称为data。我确实在“应用程序”文件夹中找到了一个“文件”文件夹,但该文件夹仅包含一个dll文件和一个Microsoft access文件(由于某些原因无法在Microsoft access中打开),您没有正确的文件资源管理器/所有安卓手机上都存在数据/数据。试试这个。不是所有的根探索者都是平等的。谢谢@Simon!我现在可以看到了。你知道android上是否有一个非root用户和应用程序可以访问的标准、公开可访问的文档文件夹吗?不,没有。应用程序只能通过
意图共享数据。以文档为中心的方法不是移动操作系统的一部分。事实上,除了一些旧的东西在Windows中向后兼容,它不是任何现代操作系统的一部分。嗨,潘迪Omi,你能帮我吗。它说“找不到类型或名称空间名称‘android’”如何在visualstudio中导入“android”名称空间?这是否仅适用于SdCard?或者也可用于内部存储?如何定义更好的位置?或者,如何获取用户资源管理器也可见的目录?
public String ReadFileData()
{
var path = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filename = Path.Combine(path.ToString(), "loginSystem.txt");
String line;
objData = new List<UsersData>();
// Read the file and display it line by line.
StreamReader file = new StreamReader(filename);
while ((line = file.ReadLine()) != null)
{
string[] words = line.Split(',');
if (words.Length != 1)
objData.Add(new UsersData(words[0], words[1], words[2]));
}
file.Close();
return String.Empty;
}
private string SaveDataToSd(String FirstName, String Address, String Password)
{
var path = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var filename = Path.Combine(path.ToString(), "loginSystem.txt");
String contents = FirstName + "," + Password + "," + Address;
try
{
using (StreamWriter data_file = new StreamWriter(filename, true))
{
data_file.WriteLine(contents);
}
return contents;
}
catch (Exception ex)
{
RunOnUiThread(() =>
{
var builder = new AlertDialog.Builder(this);
builder.SetMessage(ex.InnerException + "Saving file went wrong");
builder.SetTitle("Unable to save file");
builder.Show();
});
return String.Empty;
}
}