重新启动程序未提升
由于某种原因,我的 C# 程序需要以提升的权限重新启动。我使用以下代码来实现它:
private static void RestartForPermissionsFix()
{
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.Verb = "runas";
processInfo.FileName = Assembly.GetExecutingAssembly().Location;
Process.Start(processInfo);
}
这效果很好。
在“修复我的权限”后,我想未提升重新启动程序。我在没有“runas”的情况下尝试了与上面相同的方法,但它不起作用。我假设从提升的进程启动的进程会自动提升。有什么想法吗?
For some reason, my C# program needs to restart with elevated privileges. I use the following code to achieve it:
private static void RestartForPermissionsFix()
{
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.Verb = "runas";
processInfo.FileName = Assembly.GetExecutingAssembly().Location;
Process.Start(processInfo);
}
This works great.
After I "fix my privileges", I want to restart the program unelevated. I tried the same as above without the "runas", but it does not work. I assume the process being started from an elevated process automatically gets elevated. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了从高完整性进程启动中等完整性进程,我相信您必须使用
OpenProcessToken
,复制它,使用SetTokenInformation
,然后使用该令牌通过CreateProcessAsUser
。这与此示例类似,只是不添加您拥有的低完整性 SID删除高完整性的一个。注意:我还没有测试过这个,所以我不能 100% 确定它会起作用。我建议您让原始的未提升进程保持运行,并让它等待其提升的对应进程完成(例如使用 <代码>Process.WaitForExit)。一旦完成,它可以像以前一样继续不升高。这会容易得多并且更加万无一失。
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 usingSetTokenInformation
, and then use that token to create the new process usingCreateProcessAsUser
. 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.我对想要自动更新的应用程序遇到了同样的问题(更新程序需要提升的权限)。
我所做的是创建一个外部.exe,它将以提升的权限启动我的更新程序,等待它退出,然后以正常权限重新启动我的应用程序。
然后,我将此 .exe 嵌入到我的主应用程序中,并在更新应用程序时在离开我的应用程序之前启动此 .exe。
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.