+ Reply to Thread
Results 1 to 2 of 2

Thread: Run APP as ADMIN

  1. #1

    Run APP as ADMIN

    I had issue with windows 7 is sometimes user need to run my app as admin so that some functions could work like registry editing.

    Is there any idea i could run my app as admin even if user run it normally ?
    Last edited by csharpmaster; 11-11-2012 at 10:49 AM.

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    28
    To check if APP is running as Admin or not :
    Code:
            public Form1()
            {
                InitializeComponent();
                GetAndDisplayRights();
            }
    
            private void GetAndDisplayRights()
            {
                WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
                if (hasAdministrativeRight)
                {
                    MessageBox.Show("RUNNING AS ADMIN");
                }
            }
    To RUN any application with administrative rights :
    Code:
            private void RunElevated(string fileName)
            {
                ProcessStartInfo psi = new ProcessStartInfo();
                //psi.WindowStyle = ProcessWindowStyle.Hidden; //remove the //s if you want to run the application hidden
                psi.Verb = "runas";
                psi.FileName = fileName;
                try
                {
                    Process p = Process.Start(processInfo);
                    p.WaitForExit();
                }
                catch (Win32Exception)
                {
                    //Do nothing. Probably the user canceled the UAC window
                }
            }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts