views:

25

answers:

1

Hello,

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 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();
}
+4  A: 

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.

Stephen Cleary
Awesome! That did the trick, works great now. Thank you so much!
kcoppock
To go on: there is nothing WPF specific to the security query - Modern Windows will demand a UAC confirmation for ANY file creation in the root. Applications simply are not supposed to do that. NO application, WPF or otherwise.
TomTom