gpt4 book ai didi

c# - 无法创建文件 "c:\User\...\Appdata\Roaming..."。访问被拒绝

转载 作者:行者123 更新时间:2023-11-30 16:10:13 27 4
gpt4 key购买 nike

我想在应用生命周期内存储某些 .txt 文件。我在考虑使用 Environment.SpecialFolder.ApplicationData

所以我创造了

 string myFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), address.GetHashCode() + ".txt");
...
File.WriteAllText(myFilename, someTextContent);

我得到了

Cannot create file "c:\User...\Appdata\Roaming...". Access is denied.

最佳答案

考虑使用 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();
}
}

关于c# - 无法创建文件 "c:\User\...\Appdata\Roaming..."。访问被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26201520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com