好吧,我已经设法在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;
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);
}
}
}
};
}
}
}
}
从内部SD卡成功读取后,以下内容将输出到控制台:
目录存在。文件存在于: /data/data/ToolbarSample.ToolbarSample/files/file.txt
但是使用(许多不同的)文件管理器 - 都具有root访问权限 - 以及显示的隐藏文件 - 我无法导航到该路径,因为它不存在。我甚至做了一个完整的手机搜索&#34; file.txt&#34;并没有出现任何结果。然而,每当我打开应用程序并单击按钮时,我都能够读取该文件。
答案 0 :(得分:2)
您指定的位置的文件确实存在。您无法通过USB和文件资源管理器从PC访问该位置,但如果您使用像Root Explorer这样的优秀文件管理器应用程序,则可以访问该位置(和文件)。
如果您真的希望您的用户能够访问这些保存的文件,我建议您将这些文件保存到更好的位置,以便用户可以通过USB轻松地将文件从手机传输到计算机。
答案 1 :(得分:0)
从文件读取/写入数据非常简单。
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;
}
}