Run as administrator sometimes

I have a program that needs to be run as a regular user most of the time, but from time to time I need to stop and start the service. How can I make a program that works as a regular user most of the time, but for some function starts administrative mode?

+3
source share
3 answers

As far as I know, you need to start a separate process that starts as an administrator. You cannot pick up a process once it is already running.

See this question .

+1
source

You cannot lift a process after it starts, but you can: -

Reboot the process as elevated

private void elevateCurrentProcess()
{
    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
    {
        // User didn't allow UAC
        return;
    }
    Application.Exit();
}

, , - UAC, , , .

, exe

requireAdministrator . .

UAC .

( ) .

+2

Source: https://habr.com/ru/post/1698905/


All Articles