May 26, 2013

Execute startup application in administrator prompt or mode

Recently I came across an issue that, registry values are not added which are intends to add after executing application (Eg: TestApp.exe) which is opened on machine startup in Windows 7.  But, it is working pretty well on Win XP.
 If you have no idea how to open or setup application on windows start up follow this articleAfter discussed with colleagues tried multiple options, finally running application with compatible administrator privileges on Windows 7 works fine.

1.       Make application to run in compatible administrator privileges on Win7 manually
         - Right click on application or exe and open properties window
         - Go to compatibility tab and it looks as follows,
Click button “Change settings for all users” and it opens the following dialog,
            - Select “Run this program as administrator” option under “Privilege Level” and click OK to apply settings

Selecting this option, it will add value in registry for this application under the key 
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
with value as shown in below screen,

In the above screen we can see that, there is a value RUNASADMIN. It means that, the respective application will execute or open in administrator mode.

So now we can do same thing programmatically using any language by writing required values to registry. See the below C# example,

private void SetApplicationIntoAdminMode()
{
    RegistryKey registryKey = Registry.LocalMachine.
        OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers", true);

    // Here, first parameter should be your application path.
    registryKey.SetValue(@"D:\TestApp.exe", "RUNASADMIN");
 }