C# How to get admin privileges AT RUNTIME
Asked Answered
D

1

5

Does somebody know how to get admin privileges AT RUNTIME? Please do not suggest this:

<requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator"
          uiAccess="false"/>
</requestedPrivileges>

Hope somebody knows how to do that.

I mean, for example, consider this situation: I have an app that runs without administrator rights and usually does not makes any changes on your computer. But if you click one certain button, the app will ask for admin rights and then does something, which is needed.

Distribute answered 27/1, 2020 at 19:21 Comment(3)
What do you mean at runtime? Privileges will always be evaluated at runtime.Impresario
There are no character count restrictions here. Please spell out the words and explain your issue in detail for maximum clarity. Please visit the help center and study How to Ask for more tips on making positive contributionsRemake
@Mathews SomeBodyGoodill
S
9

You can't change the privilege level of a currently running process. You CAN launch a new process, asking for elevated permissions. Process.Start() supports the flag "runas".

        using (Process configTool = new Process())
        {
            configTool.StartInfo.FileName = "foo.exe"
            configTool.StartInfo.Arguments = "--bar";
            configTool.StartInfo.Verb = "runas";
            configTool.Start();
            configTool.WaitForExit();
        }

See Elevating process privilege programmatically?

You could separate functions of your application into a second executable, or re-launch your application with elevated permissions with "runas".

Serious answered 27/1, 2020 at 19:37 Comment(1)
A post which nothing more than a link to some other post is not considered an answer at all.Remake

© 2022 - 2024 — McMap. All rights reserved.