我正在使用Xamarin,根据之前的回答,应该可以解决:
string path = Path.Combine(Android.OS.Environment.DirectoryDownloads, "families.txt");
File.WriteAllText(path, "Write this text into a file!");
但是它并没有成功,我收到了一个未处理的异常。我已经设置了读写外部存储的权限(即使这是内部存储)。
我也尝试了以下代码:
string content;
using (var streamReader = new StreamReader(@"file://" + path )) // with and without file://
{
content = streamReader.ReadToEnd();
}
但我得到了相同的未处理异常。
更新:路径是问题所在,因为我在此处得到了 else 部分:
Java.IO.File file = new Java.IO.File(path);
if (file.CanRead())
testText.Text = "The file is there";
else
testText.Text = "The file is NOT there\n" + path;
这很奇怪,因为路径看起来是正确的。异常信息:无法找到路径的一部分:/ Download / families.txt
更新2:在外部存储中,使用相同的代码可以正常工作...可能是我的设备有问题吗?那太好了,因为我已经在我朋友的手机上测试了外部存储版本,但我的手机没有外部存储(OnePlus One),所以我仍在寻找解决方案(如果有的话)。