user175084
user175084

Reputation: 4630

Giving application elevated UAC

I have an application which needs the UAC elevation.

I have the code which lets me give that but the application opens twice and that's an issue.

Here's the code of Form1:

public Form1()
{
    InitializeComponent();

    WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
    bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);           

    if (!hasAdministrativeRight)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.UseShellExecute = true;
        startInfo.WorkingDirectory = Environment.CurrentDirectory;
        startInfo.FileName = Application.ExecutablePath;
        startInfo.Verb = "runas";
        
        try
        {
            Process p = Process.Start(startInfo);
        }
        catch (System.ComponentModel.Win32Exception ex)
        {
            return;
        }
    }
}

Here's the code of programs.cs:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

By debugging I found out that first it executes

Process p = Process.Start(startInfo);

which opens the application UAC elevation dialog and then opens the application

but then it goes to the

Application.Run(new Form1());

in main() and opens the application again.

I don't want it to open the app again.

I am new to this so I am asking is there anything I am doing wrong and do I need to close the UAC once its open?

Upvotes: 14

Views: 49413

Answers (4)

Bueller
Bueller

Reputation: 2344

This is a much better approach when your application is known to require Admin privileges from the start.

Upvotes: 3

Martyn Talbot
Martyn Talbot

Reputation: 61

Move the WindowsPrincipal code from your Form to Program.cs as in the example below. This will prompt the user for UAC authority prior to launching any forms and will only launch the form if UAC authority has been granted.

        static void Main()
        {
            WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);

            if (!hasAdministrativeRight)
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.UseShellExecute = true;
                startInfo.WorkingDirectory = Environment.CurrentDirectory;
                startInfo.FileName = Application.ExecutablePath;
                startInfo.Verb = "runas";
                try
                {
                    Process p = Process.Start(startInfo);
                    Application.Exit();
                }
                catch (System.ComponentModel.Win32Exception ex)
                {
                    MessageBox.Show("This utility requires elevated priviledges to complete correctly.", "Error: UAC Authorisation Required", MessageBoxButtons.OK);
//                    Debug.Print(ex.Message);
                    return;
                }
            }
            else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }

Upvotes: 6

Jon
Jon

Reputation: 437376

You don't need to meddle with all that to make sure that your application always runs with elevated privileges. You can simply add an application manifest which instructs Windows to run your app elevated, and the UAC prompt will appear without you needing to write a single line of code.

There's a related question with an answer that also describes how to add a manifest here: How can I embed an application manifest into an application using VS2008?

Upvotes: 32

J. Steen
J. Steen

Reputation: 15578

Elevating your privileges is always going to start a new process. There is no way around that, other than starting with elevated privileges in the first place by setting your application to require administrative privileges. What you can do is end the application right after the elevated process starts, so that you only have one application running.

This scenario is usable for applications that only require certain parts of their function to be elevated - such as an automatically self-updating installer that needs access to Program Files - and not one that requires administrative access all the time.

Upvotes: 3

Related Questions