[英]Xamarin.Mac OS X save and retrieve data from file
I'm new to Xamarin.Mac desktop development. 我是Xamarin.Mac桌面开发的新手。
I'm building a OS X based application and I need to save and retrieve some text data within my app. 我正在构建一个基于OS X的应用程序,我需要在我的应用程序中保存和检索一些文本数据。
I tried the same approach that we use in windows like, create a file and save data into that file and modify or retrieve as you need. 我尝试了在Windows中使用的相同方法,例如创建文件并将数据保存到该文件中,然后根据需要进行修改或检索。
But thing in macOS seems different, My Application is creating file but I can only read data from that file. 但是macOS中的情况似乎有所不同,我的应用程序正在创建文件,但我只能从该文件读取数据。 Permission Issue.
权限问题。 (can't write to file without admin rights).
(如果没有管理员权限,则无法写入文件)。
I have tried several approaches, here's one of those approaches (I tried to remove readonly permissions from code while creating it). 我尝试了几种方法,这是其中一种(我试图在创建代码时从代码中删除只读权限)。 How to remove a single Attribute (eg ReadOnly) from a File?
如何从文件中删除单个属性(例如ReadOnly)?
But result is same. 但是结果是一样的。
I wanted to know is there any other approach that can allow my app to read and write data into that? 我想知道是否有其他方法可以让我的应用读取数据并将数据写入其中?
Edit: Code 编辑:代码
string path = Path.Combine(Directory.GetCurrentDirectory(), fileName);
if (File.Exists(path))
{
using (FileStream aFile = new FileStream(path, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(aFile))
{
sw.WriteLine(fileData);
}
}
}
else
{
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine(fileData);
}
}
You cannot just write to any path. 您不能只写任何路径。 Try to user documents folder that should work.
尝试使用应该工作的用户文档文件夹。
var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine (documents, "Write.txt");
File.WriteAllText(filename, "Write this text into a file");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.