+ Reply to Thread
Results 1 to 2 of 2

Thread: Run application as admin on startup of application [Enhanced Function]

  1. #1

    Run application as admin on startup of application [Enhanced Function]

    I just recently used windows 7 and windows 7 has more security so if you want to open application that perform certain functions that requires an admin access , so application must be opened as admin first.

    So how to do that in code ?
    Last edited by csharpmaster; 11-09-2012 at 12:08 AM.

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    36
    I Have previously discussed that HERE But i figured a better code , the one i previously discussed will mostly run other application as Admin , but it is important to read before that tutorial.

    I have Figured 3 Steps to run any application you create as administrator.
    1- Check if it is running as Admin
    2- IF it is running as admin then load the Form
    3- If not , then run the same application but as Admin and then close the current Application

    So , think of it what is the Very beginning Entry of the application ? in other words > What Application always read first before Initializing the Form?
    It is the Program.cs , when you double click it you will notice that it is a console application but it is not , it just load the form this is the entry point , so our code will be there , we do not want to load form and then check if the application is running as admin or not , because simply your Form could need to be run as admin in order to work , so that would be illogical.

    So at the Beginning we will add 3 NameSpaces :
    Code:
    using System.Security.Principal;
    using System.Threading;
    using System.Diagnostics;
    Then inside Main check that :
    Instead of :
    Code:
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
    It will be :
    Code:
                AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
                WindowsPrincipal currentPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
    
                if (currentPrincipal.IsInRole("Administrators"))
                {
                    Application.Run(new Form1());
                }
                else
                {
                    // open your program as admin using process
                    ProcessStartInfo psi = new ProcessStartInfo("ApplicationName.exe");
                    psi.UseShellExecute = true;
                    // runas opens it as Administrator.
                    psi.Verb = "runas";
                    Process p = Process.Start(psi);
                    // kill the current application
                    Application.Exit();
                }

    But after applying that method i had an application to run at Windows Startup and now it does not work , i don`t know if the problem is on that method or not , but once i figure it out i`ll update the post, i just wanted to share that in order to be aware of any upcoming errors.

    Finally hope that it is informative for you.

+ 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