[英]Xamarin.Mac OS X save and retrieve data from file
我是Xamarin.Mac桌面開發的新手。
我正在構建一個基於OS X的應用程序,我需要在我的應用程序中保存和檢索一些文本數據。
我嘗試了在Windows中使用的相同方法,例如創建文件並將數據保存到該文件中,然后根據需要進行修改或檢索。
但是macOS中的情況似乎有所不同,我的應用程序正在創建文件,但我只能從該文件讀取數據。 權限問題。 (如果沒有管理員權限,則無法寫入文件)。
我嘗試了幾種方法,這是其中一種(我試圖在創建代碼時從代碼中刪除只讀權限)。 如何從文件中刪除單個屬性(例如ReadOnly)?
但是結果是一樣的。
我想知道是否有其他方法可以讓我的應用讀取數據並將數據寫入其中?
編輯:代碼
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);
}
}
您不能只寫任何路徑。 嘗試使用應該工作的用戶文檔文件夾。
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.