重新启动程序未提升

发布于 2024-11-24 13:24:01 字数 442 浏览 1 评论 0原文

由于某种原因,我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光①夏 2024-12-01 13:24:01

为了从高完整性进程启动中等完整性进程,我相信您必须使用 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 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.

晨光如昨 2024-12-01 13:24:01

我对想要自动更新的应用程序遇到了同样的问题(更新程序需要提升的权限)。

我所做的是创建一个外部.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文