Running as Administrator with Click Once Application on Windows 8

You have probably noticed that with windows 8 when you disable UAC it doesn’t fully turn off. You are able to completely turn it off but that then disables the ability to use any of the Modern UI applications. So today I dug a bit and eventually managed to get a solution to make your application run as admin when being run from a click once deploy.

The way a user would be able to enable admin mode for a regular application is to Right click on the application and click ‘Run as administrator’


With click once applications you aren’t able to do this. I suppose you could find one of the many ways to locate the actual executable and then run as administrator but then every time the application updates you will need to locate the application again and also many users (including myself) won’t see this as a suitable way to launch an application as administrator.

After trying all the regular methods of enabling an application to run as full administrator I eventually got to the solution below.

Add these usings into the program.cs file

   1: using System.ComponentModel;

   2: using System.Linq;

Add the member and method to the program.cs class

   1: private const uint BCM_SETSHIELD = 0x160C;


   3: [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]

   4: private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, IntPtr lParam);

Add the parameter string[] args to the main method if it does not already exist

   1: public static void Main(string[] args)

And finally wrap all the code inside you main method with

   1: if (string.IsNullOrEmpty((from o in args where o == "--engage" select o).FirstOrDefault()))

   2: {

   3:     var btnElevate = new Button();

   4:     btnElevate.FlatStyle = FlatStyle.System;


   6:     SendMessage(btnElevate.Handle, BCM_SETSHIELD, 0, (IntPtr) 1);


   8:     var processInfo = new ProcessStartInfo();

   9:     processInfo.Verb = "runas";

  10:     processInfo.FileName = Application.ExecutablePath;

  11:     processInfo.Arguments = string.Join(" ", args.Concat(new[] { "--engage" }).ToArray());

  12:     try

  13:     {

  14:         Process p = Process.Start(processInfo);

  15:         p.WaitForExit();

  16:     }

  17:     catch (Win32Exception)

  18:     {

  19:         //Do nothing. Probably the user cancelled the UAC window or provided invalid credentials.

  20:     }


  22:     Application.Exit();

  23: }

  24: else

  25: {

  26:     // place code that was in the main method here

  27: }

That’s all you need to be able to run your application as administrator on launch. Basically what is going to happen is your application will start up and see that there is no command argument for –engage, it will then get it’s own executable path and attempt to run itself again using administrator mode. If a user has UAC enabled they will be prompted as usual to allow the application to run in admin mode and if they have UAC disable in windows 8 the application will now run if real administrator mode.


One thought on “Running as Administrator with Click Once Application on Windows 8

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s