授予WPF应用访问本地驱动器的权限?

时间:2010-06-26 04:13:31

标签: c# wpf security permissions streamwriter

我正在用C#编写一个应用程序(这对我的朋友来说非常基本),但我有一个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();
}

1 个答案:

答案 0 :(得分:4)

实际上,您应该写入本地用户的“应用程序设置”或“文档”文件夹,而不是根驱动器。使用Environment.SpecialFolder中的ApplicationDataPersonal