Captain Comic
Captain Comic

Reputation: 16196

UnauthorizedAccessException when writing text file to C:\ on Windows 7

I have recently moved to Windows 7. Now my .NET application fails writing log file to C:\

My TraceListener is throwing the exception.

A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll

What to do? I am running application from the studio and so I think it must inherit all my rights and I am the admin on my pc.

Upvotes: 2

Views: 2446

Answers (3)

Grant Thomas
Grant Thomas

Reputation: 45083

Your program will need to run with the required permissions. Only those with Administrator access can write to 'special' folders such as the system drive and so the application will need to execute initially with elevated privileges.

See here, and here for information on security permissions in code.

Upvotes: 2

leppie
leppie

Reputation: 117220

You probably have UAC still turned on.

You will need to indicate in your app.config, that you want the process to use elevated rights.

The default generated app.config have that info.

Upvotes: 0

Anders Abel
Anders Abel

Reputation: 69260

If you have UAC enabled you won't be able to write files to C:\, even if you're admin, unless you start the program in elevated mode to activate the admin privileges.

Files shouldn't really be placed in the root of C:, so the best is to create a subdirectory and give yourself access rights (to your account, not the administrators group). If you really want to have the file on C:\ and not run it as elevated, you can use Windows Explorer to grant yourself (your account, not the administrators group) write access to C:\.

Upvotes: 3

Related Questions