我正在用 C# 编写一个应用程序(对于我的一个 friend 来说,这是非常基础的),但是我有一个 StreamWriter 对象,它在 C: 中创建一个本地文件。我必须以管理员身份运行,它工作正常,但否则它会崩溃并显示“访问路径 'C:\final.html' 被拒绝。”
我以前从未在代码中使用过任何类型的安全或权限。我可以做些什么来授予它对该文件夹的访问权限而无需以管理员身份运行?
编辑:这是我最后做的:
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
using (StreamWriter output = new StreamWriter(Path.Combine(desktop, "final.html"))
{
//...additional code
output.Write(inputArray.Substring(lastIndex, range.Start - lastIndex));
//...additional code
output.Close();
}
请您参考如下方法:
实际上,您应该写入本地用户的应用程序设置或文档文件夹,而不是根驱动器。使用来自 Environment.SpecialFolder 的 ApplicationData 或 Personal .
