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
.
精彩评论