重新启动程序未提升 [英] Restart program unelevated

查看:109
本文介绍了重新启动程序未提升的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我的C#程序需要以提升的特权重新启动.我使用以下代码来实现它:

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);
}

这很好.

固定权限"后,我想重新启动程序未提升.我尝试了与上述相同的操作,但没有"runas",但它不起作用.我假设从提升流程开始的流程会自动提升.有什么主意吗?

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?

推荐答案

为了从高完整性进程以中等完整性启动进程,我相信您必须使用 SetTokenInformation ,然后使用该令牌通过此示例,不同之处在于它不是添加您将拥有的低完整性SID.去除高完整性之一.注意:我尚未对此进行测试,所以我不是100%确信它会起作用.

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.

这篇关于重新启动程序未提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆