开发者

Grant WPF application access to local drive?

开发者 https://www.devze.com 2023-01-05 06:26 出处:网络
I\'m writing an application in C# (it\'s very basic, for a friend of mine), but I have a StreamWriter object that creates a local file in C:. I have to do Run as Administrator, and it works fine, but

I'm writing an application in C# (it's very basic, for a friend of mine), but I have a StreamWriter object that creates a local file in C:. I have to do Run as Administrator, and it works fine, but otherwise it crashes with "Access to the path 'C:\final.html' is denied."

I've never worked with any sort of开发者_Go百科 security or permissions in code before. What can I do to grant it access to that folder without having to run as administrator?

EDIT: Here's what I ended up doing:

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();
}


Actually, you should be writing to the local user's Application Settings or Documents folder, not the root drive. Use ApplicationData or Personal from Environment.SpecialFolder.

0

精彩评论

暂无评论...
验证码 换一张
取 消