C# 无法创建文件";c:\User\…\Appdata\Roaming…“用户”;。访问被拒绝
我想在应用程序生命周期内存储某些.txt文件。我正在考虑使用C# 无法创建文件";c:\User\…\Appdata\Roaming…“用户”;。访问被拒绝,c#,.net,C#,.net,我想在应用程序生命周期内存储某些.txt文件。我正在考虑使用Environment.SpecialFolder.ApplicationData 所以我创造了 string myFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), address.GetHashCode() + ".txt"); ... File.WriteAllText(myFilename,
Environment.SpecialFolder.ApplicationData
所以我创造了
string myFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), address.GetHashCode() + ".txt");
...
File.WriteAllText(myFilename, someTextContent);
我要走了
无法创建文件“c:\User…\Appdata\Roaming…”。访问被拒绝。
考虑使用,您可以通过它保证读/写访问
写作:
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();
}
}
你需要在那里写字吗?您可以写入临时目录(假设它是
Path.GetTempDirectory()
)或其他地方吗?您的代码是如何执行的?它是在windows帐户下运行的命令行吗?它是windows服务吗?作为服务帐户运行的IIS托管网站?@PhilipPittle这是一个简单的wpf应用程序。