我想在应用生命周期内存储某些.txt文件。我在考虑使用Environment.SpecialFolder.ApplicationData
所以我创建了
string myFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), address.GetHashCode() + ".txt");
...
File.WriteAllText(myFilename, someTextContent);
我正在
无法创建文件" c:\ User ... \ Appdata \ Roaming ..."。访问被拒绝。
答案 0 :(得分:3)
考虑使用Isolated Storage,您可以保证使用它进行读/写访问。
写作:
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew, isoStore))
{
using (StreamWriter writer = new StreamWriter(isoStream))
{
writer.WriteLine("Hello Isolated Storage");
}
}
读:
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.Open, isoStore))
{
using (StreamReader reader = new StreamReader(isoStream))
{
string contents = reader.ReadToEnd();
}
}