Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.. which is the issue..

so here is the code in 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;
            }

        }

    }

and this is the code programs.cs

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

on debugging i find 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 dont want it to open the app again...

i am new to this is there anything i am doing wrong and do i need to close the UAC once its open..

thanks

like image 365
user175084 Avatar asked Jun 20 '11 14:06

user175084


People also ask

How do I run a program elevated without UAC prompt?

You can run apps elevated (as administrator) without getting the UAC elevation prompt when logged in to an administrator account. The trick to bypass UAC is to create a scheduled task (with highest privileges) for each program that you want to run, and then invoke the scheduled task item manually using schtasks.exe .

How do I allow a program to run without UAC?

run-app-as-non-admin.bat After that, to run any application without the administrator privileges, just select “Run as user without UAC privilege elevation” in the context menu of File Explorer. You can deploy this option to all computers in the domain by importing the registry parameters using GPO.


2 Answers

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());
            }
like image 152
Martyn Talbot Avatar answered Oct 07 '22 20:10

Martyn Talbot


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?

like image 33
Jon Avatar answered Oct 07 '22 22:10

Jon