Allow access permission to write in Program Files of Windows 7

Solution 1:

Your program should not write temporary files (or anything else for that matter) to the program directory. Any program should use %TEMP% for temporary files and %APPDATA% for user specific application data. This has been true since Windows 2000/XP so you should change your aplication.

The problem is not Windows 7.

You can ask for appdata folder path:

string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

or for TEMP path

string dir = Path.GetTempPath()

Solution 2:

Your program has to run with Administrative Rights. You can't do this automatically with code, but you can request the user (in code) to elevate the rights of your program while it's running. There's a wiki on how to do this. Alternatively, any program can be run as administrator by right-clicking its icon and clicking "Run as administrator".

However, I wouldn't suggest doing this. It would be better to use something like this:

Environment.GetFolderPath(SpecialFolder.ApplicationData);

to get the AppData Folder path and create a folder there for your app. Then put the temp files there.

Solution 3:

Options I can think of:

  • Run entire app as full admin priv. using UAC
  • Run a sub-process as full admin for only those things needing access
  • Write temporary files elsewhere

Solution 4:

Add new item in the project: Application Manifest and save it.

Now open this file and look for <requestExecutionLevel>. It must be set to asInvoker.

Change it to highestAvailable. Now on executing your application, a prompt will appear asking for permission. Click yes!

Thats all :) now you can write and read from the system32 or any other file which requires admin right

You can verify your application by sigcheck.

sigcheck.exe -m yourapp.exe

And in the output check for element requestedExecutionLevel.