Click here to Skip to main content
15,919,500 members
Articles / Programming Languages / C#

Running as Administrator with Click Once Application on Windows 8

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
28 Sep 2013CPOL2 min read 30.3K   1   10
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’.

image

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:

C#
1: using System.ComponentModel;
2: using System.Linq;

Add the member and method to the program.cs class:

C#
1: private const uint BCM_SETSHIELD = 0x160C;
2:
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:

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

And finally wrap all the code inside your main method with:

C#
 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;
 5:
 6:     SendMessage(btnElevate.Handle, BCM_SETSHIELD, 0, (IntPtr) 1);
 7:
 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:     }
21:
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 its 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 disabled in Windows 8, the application will now run in real administrator mode.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect SSW
South Africa South Africa

Comments and Discussions

 
Questionupdates are not working Pin
33deepak25-Feb-16 9:12
33deepak25-Feb-16 9:12 
this method works fine to run the app as admin. But as it is now running as admin, successive updates are not happening automatically.
QuestionNot working for me Pin
Member 86367783-Oct-13 5:35
Member 86367783-Oct-13 5:35 
AnswerRe: Not working for me Pin
Gordon Beeming3-Oct-13 5:52
professionalGordon Beeming3-Oct-13 5:52 
GeneralRe: Not working for me Pin
Member 86367783-Oct-13 5:57
Member 86367783-Oct-13 5:57 
GeneralRe: Not working for me Pin
Gordon Beeming3-Oct-13 6:02
professionalGordon Beeming3-Oct-13 6:02 
GeneralRe: Not working for me Pin
Member 86367783-Oct-13 6:14
Member 86367783-Oct-13 6:14 
GeneralRe: Not working for me Pin
Gordon Beeming3-Oct-13 6:17
professionalGordon Beeming3-Oct-13 6:17 
GeneralRe: Not working for me Pin
Member 86367783-Oct-13 8:36
Member 86367783-Oct-13 8:36 
GeneralRe: Not working for me Pin
Gordon Beeming3-Oct-13 8:58
professionalGordon Beeming3-Oct-13 8:58 
GeneralRe: Not working for me Pin
Member 86367783-Oct-13 11:05
Member 86367783-Oct-13 11:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.