Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

c# run program as an administrator

// Program

        public static bool IsAdministrator()
        {
                WindowsIdentity identity = WindowsIdentity.GetCurrent();
                WindowsPrincipal principal = new WindowsPrincipal(identity);
                return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }


// Form1

        private void Form1Shown(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.Run1();
            this.Cursor = Cursors.Default;
        }

        private void Run1()
        {
            if (!Program.IsAdministrator())
            {
                // Restart and run as admin
                var exeName = Process.GetCurrentProcess().MainModule.FileName;
                ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
                startInfo.Verb = "runas";
                startInfo.Arguments = "restart";
                Process.Start(startInfo);
                Application.Exit();
            }
		}
Source by www.codeproject.com #
 
PREVIOUS NEXT
Tagged: #run #program #administrator
ADD COMMENT
Topic
Name
5+1 =