Example #1
0
        public static void AttemptPrivilegeEscalation(string path, EscalationEvent starting, EscalationEvent cancelled, EscalationEvent successful)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            if (!File.Exists(path))
            {
                throw new FileNotFoundException("path");
            }

            if (HasAdminPrivileges())
            {
                throw new SecurityException("Already have administrator privileges.");
            }

            ProcessStartInfo startInfo = EscalationProcessStartInfo(path);

            if (starting != null)
            {
                starting();
            }

            ////todo: slight issue with this... when the program is in admin status...
            ////if my EscalationCustomProcessPath is set to some random program that goes online right away
            ////i get norton saying this application program (not the other program) is trying to connect to the internet...

            try
            {
                Process.Start(startInfo);
            }

            catch (System.ComponentModel.Win32Exception) //occurs when the user has clicked Cancel on the UAC prompt.
            {
                if (cancelled != null)
                {
                    cancelled();
                }

                return; // By returning, we are ignoring the user tried to get UAC priviliges but then hit cancel at the "Run-As" prompt.
            }

            if (successful != null)
            {
                successful();
            }
        }
        public static void AttemptPrivilegeEscalation(string path, EscalationEvent starting, EscalationEvent cancelled, EscalationEvent successful)
        {
            if (String.IsNullOrEmpty(path))
                throw new ArgumentNullException("path");

            if (!File.Exists(path))
                throw new FileNotFoundException("path");

            if (HasAdminPrivileges())
                throw new SecurityException("Already have administrator privileges.");

            ProcessStartInfo startInfo = EscalationProcessStartInfo(path);

            if(starting != null)
                starting();

            ////todo: slight issue with this... when the program is in admin status...
            ////if my EscalationCustomProcessPath is set to some random program that goes online right away
            ////i get norton saying this application program (not the other program) is trying to connect to the internet...

            try
            {
                Process.Start(startInfo);
            }

            catch (System.ComponentModel.Win32Exception) //occurs when the user has clicked Cancel on the UAC prompt.
            {
                if(cancelled != null)
                    cancelled();

                return; // By returning, we are ignoring the user tried to get UAC priviliges but then hit cancel at the "Run-As" prompt.
            }

            if(successful != null)
                successful();
        }