Running a ClickOnce Application as Administrator

WPF & WinForms Posted on 12 652 views 3 comments

Using ClickOnce to deploy your Windows application is the easiest way you can imagine. With few clicks, you can deploy your application without any aditional effort, and the most important thing is the simplicity of re-deploying new versions of your applications to end-user.

BUT, as it gets with all automatic process, there are situations when you ran into a problem: my ClickOnce deployed application wouldn't start on Windows 8 operating system.

The application worked great on all pre-versions of Windows like XP, Vista and 7, but the problem were Windows 8. Non of the solutions, like checking "Run this program as an administrator" in EXE's properties haven't worked for me.

ClickOnce allows you to install application even if the user does not have admin rights:
"ClickOnce deployment enables non-administrative users to install and grants only those Code Access Security permissions necessary for the application".
(For more info please visit: http://msdn.microsoft.com/en-us/library/t71a733d(v=vs.100).aspx)

So the fix was to restart current application with administrator privileges!

First we need to do is to import following namespaces.
NOTE: for "System.Management" you will have to add reference to your project for System.Management.dll.

using System.Security.Principal;
using System.Management;
using System.Diagnostics;
using System.Reflection;
    

The second part of code is the main code. Here, we check if user is running as administrator. If it's not administrator, then do the folowing code: restart current process in administrator mode.

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
    // Check if user is NOT admin
    if (!IsRunningAsAdministrator())
    {
        // Setting up start info of the new process of the same application
        ProcessStartInfo processStartInfo = new ProcessStartInfo(Assembly.GetEntryAssembly().CodeBase);

        // Using operating shell and setting the ProcessStartInfo.Verb to “runas” will let it run as admin
        processStartInfo.UseShellExecute = true;
        processStartInfo.Verb = "runas";

        // Start the application as new process
        Process.Start(processStartInfo);

        // Shut down the current (old) process
        System.Windows.Forms.Application.Exit();
    }
}

/// <summary>
/// Function that check's if current user is in Aministrator role
/// </summary>
/// <returns></returns>
public static bool IsRunningAsAdministrator()
{
    // Get current Windows user
    WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

    // Get current Windows user principal
    WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);

    // Return TRUE if user is in role "Administrator"
    return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
    

This solution works on all cases, where you must run current application as administrator.