我正在尝试使用Xamarin将文件写入手机的内部存储中。到目前为止,我尝试遵循this tutorial并发现了that个相关的问题。我的代码如下:
public async Task WriteDataToFile() {
string PersonalFolderPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
System.Diagnostics.Debug.WriteLine("PersonalFolderPath: " + PersonalFolderPath);
var backingFile = Path.Combine(PersonalFolderPath, "count.txt");
System.Diagnostics.Debug.WriteLine("backingFile: " + backingFile.ToString());
if (!System.IO.File.Exists(backingFile)) {
var newfile = new Java.IO.File(backingFile);
using (FileOutputStream outfile = new FileOutputStream(newfile)) {
string line = "The very first line!";
outfile.Write(System.Text.Encoding.ASCII.GetBytes(line));
outfile.Close();
}
}
}
这是我收到的调试输出:
[0:] PersonalFolderPath:/data/user/0/MyApp.Android/files
[0:] backingFile:/data/user/0/MyApp.Android/files/count.txt
执行代码后,我尝试通过检查路径“ This PC \ Galaxy S8 \ Phone \ Android \ data \ MyApp.Android \ files”来从我的PC访问新创建的文件,但不幸的是它不存在。我在这里做错了什么?非常感谢您的帮助。