Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow access permission to write in Program Files of Windows 7

Tags:

c#

People also ask

How do I give permission to program files?

Step 1: Right-click the folder you want to save files to and select Properties from the context menu. Step 2: Select Security tab in the pop-up window, and click Edit to change permission. Step 3: Select Administrators and check Full control in Allow column. Then click OK to save the changes.

How do I fix permissions denied in Windows 7?

Right-click the file or folder, and then click Properties. Click the Security tab. Under Group or user names, click your name to see the permissions you have. Click Edit, click your name, select the check boxes for the permissions that you must have, and then click OK.

How do I enable write permissions in Windows?

Through Windows Explorer Navigate to the file or folder that needs its permissions changed. Right-click on the file or folder and click on Properties. Select the Security tab. Click on the Internet Guest Account and make sure that the Allow checkbox is set for Write permissions.


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

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.


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

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.


Another way round it would be to stop UAC then restart it. Create a CMD file with the following code;

Rem Stop UAC %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f rem force reboot Start ShutDown /R /F /T 30

You'll need to right click on the CMD file and use run as admin. once you have finished what you are doing restart UAC with the following code (no need to use run as admin this time);

%windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 1 /f

rem force reboot Start ShutDown /R /F /T 30

The down sides to using this method is have to right click and use run as admin to close UAC down and you have to reboot for it to take effect.

BTW there are several reasons why you would need to write to the forbidden areas...the first two that springs to mind would be to run a batch file to append host to prevent your browser going to dodgy sites or to copy license keys in a silent install.


You can't cause a .Net application to elevate its own rights. It's simply not allowed. The best you can do is to specify elevated rights when you spawn another process. In this case you would have a two-stage application launch.

Stage 1 does nothing but prepare an elevated spawn using the System.Diagnostics.ProcessStartInfo object and the Start() call.

Stage 2 is the application running in an elevated state.

As mentioned above, though, you very rarely want to do this. And you certainly don't want to do it just so you can write temporary files into %programfiles%. Use this method only when you need to perform administrative actions like service start/stop, etc. Write your temporary files into a better place, as indicated in other answers here.