For a while now the installer for my program has used the below code to make my app run with administer privileges. But it seems to have no effect under Windows 7. In Vista it worked beautifully. If I right click the shortcut and tell it to run as Administer, the program will start fine. But by using the below, code it should be made to run the program that way all the time. It doesn't anymore. Does anyone know if Win 7 still uses this key? UAC is also on by the way.
Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\
CurrentVersion\AppCompatFlags\Layers", "C:\App\app.exe", "RUNASADMIN");
Thanks.
I have an answer/workaround for this question.
First off, I disagree (respectfully) with the comment that using the AppCompatFlags is not a "proper way to configure your application and installer." Modifying this section of the registry is simply mirroring using the Windows GUI to change the Privilege Level of the executable. I find this method easier to implement than adding a manifest file. If the user wants or needs to change the Privilege Level to not Run as Administrator, they can do that easily with the GUI.
Anyway, I had this same problem of trying to set the Privilege Level of the executable to Run as administrator. We know that we can set it with the the GUI:
When the changes are saved, you will find the setting in the registry:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Program Files (x86)\\My Program\\My Program.exe"="RUNASADMIN"
When I set the .exe
to run as administrator using the GUI in this way, it always works.
However, whenever I tried to change the registry directly without going through the GUI, the program just won't run as administrator. The registry shows that I made the change and when I look at the Privilege Level for the executable, Run as administrator is checked as on.
I tried several different ways to make the .exe
run as an administrator by just changing the registry:
.reg
fileAll these methods did the same thing. The registry was changed and the GUI showed the that program should run as an administrator, but the program never runs as an administrator.
The fix that for this problem that I stumbled across is to go ahead and change both the HKCU
key and the HKLM
key with the setting.
[HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Program Files (x86)\\My Program\\My Program.exe"="RUNASADMIN"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
"C:\\Program Files (x86)\\My Program\\My Program.exe"="RUNASADMIN"
If you change both of these registry sections, then the .exe
will run as an administrator. More importantly, if a different user logs in to the the PC, the program will run as an administrator. This is in spite of the registry change not being made HKCU section for the subsequent user.
I don't know what is going on here, but it is working.
I am using Windows 7 and I can see such keys. However, I don't think that's a proper way to configure your application and installer.
My recommendation is that you distribute a manifest file along with your application (app.exe). The manifest file can be even embedded in the executable easily if you are using Visual Studio 2008. Then in the manifest file you can require administrator rights.
http://msdn.microsoft.com/en-us/library/bb756929.aspx
http://blogs.msdn.com/shawnfa/archive/2006/04/06/568563.aspx
http://channel9.msdn.com/posts/jmazner/How-To-Tell-Vistas-UAC-What-Privelege-Level-Your-App-Requires/
in win7 , RUNASADMIN
IS PLACED IN KEY :
when HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers
using install shield 5.1 , the values are copied to the appcpmctflgsin wow6432node
and exe
actualy falis to run as admin.
This answer by RobeN works "for an exe file I didn't create," which you expressed as an interest in your comment on Lex Li's answer. It uses your original registry idea.
Two possibly relevant differences:
Wow6432Node
(a concern mentioned by "uss")HKLM
instead of HKCU
, I need not worry about which user is executing the application (a concern mentioned by Leo).I used CMD for adding this entry into Registry using following command:
reg ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /t REG_SZ /f /v "C:\Program Files (x86)\MyApp\myapp.exe" /d RUNASADMIN
This works good in Win8Pro-32Bit but not works on 64Bit version!
I found that running this command on a 64Bit Win (runs through a 32-bit installer as final install stage), causes to creating entry on HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
!
After some research (thank's to my friend Mr. H.Toosi), we found right solution.
JUST ADD /reg:64
AT END OF THE EARLIER COMMAND like this:
reg ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /t REG_SZ /f /v "C:\Program Files (x86)\MyApp\myapp.exe" /d RUNASADMIN /reg:64
and everything is normal in both 32 and 64 bit OSes(Win 7 32Bit, Win 8 32Bit, Win 8.1 64Bit).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With