Restart program unelevated

后端 未结 2 680
后悔当初
后悔当初 2021-01-05 06:22

For some reason, my C# program needs to restart with elevated privileges. I use the following code to achieve it:

private static void RestartForPermissionsFi         


        
相关标签:
2条回答
  • 2021-01-05 06:49

    I had the same problem with an application that I wanted to update automatically (The update program requires elevated privileges).

    What I did was creating an external .exe that would start my updater program with elevated privileges, wait for it to exit, then restart my application with normal privileges.

    I then embedded this .exe in my main application, and start this .exe just before leaving my application when I update it.

    0 讨论(0)
  • 2021-01-05 06:58

    In order to launch a process at medium integrity from a high integrity process, I believe you would have to get the current process token using OpenProcessToken, duplicate it, remove the high integrity SID from the token using SetTokenInformation, and then use that token to create the new process using CreateProcessAsUser. This would be similar to this example, except rather than add the low integrity SID you'd have to remove the high integrity one. Note: I haven't tested this, so I'm not 100% sure it would work.

    I suggest you leave the original unelevated process running, and have it wait for its elevated counterpart to finish (e.g. using Process.WaitForExit). Once that finishes, it can continue unelevated as before. This would be a lot easier and more foolproof.

    0 讨论(0)
提交回复
热议问题