简体   繁体   English

当Verb =“runas”时设置ProcessStartInfo.EnvironmentVariables

[英]Set ProcessStartInfo.EnvironmentVariables when Verb=“runas”

I am developing a C# application. 我正在开发一个C#应用程序。

I need to create and pass variables to a new process and I am doing it using ProcessStartInfo.EnvironmentVariables . 我需要创建变量并将变量传递给新进程,我正在使用ProcessStartInfo.EnvironmentVariables来完成它。

The new process must run elevated so I am using Verb = "runas" 新进程必须提升,所以我使用的是Verb =“runas”

var startInfo =  new ProcessStartInfo(command)
{
    UseShellExecute = true,
    CreateNoWindow = true,
    Verb = "runas"
};
foreach (DictionaryEntry entry in enviromentVariables)
{
    startInfo.EnvironmentVariables.Add(entry.Key.ToString(), entry.Value.ToString());
}

The problem is that according to the msdn documentation : 问题是根据msdn文档

You must set the UseShellExecute property to false to start the process after changing the EnvironmentVariables property. 必须将UseShellExecute属性设置为false才能在更改EnvironmentVariables属性后启动该进程。 If UseShellExecute is true, an InvalidOperationException is thrown when the Start method is called. 如果UseShellExecute为true,则在调用Start方法时抛出InvalidOperationException

but the runas variable requires UseShellExecute=true runas变量需要UseShellExecute = true

Is there a way to do both: run process as elevated and also set the environment variables? 有没有办法同时执行:将进程作为提升运行并设置环境变量?

EDIT 编辑

I will try to rephrase my question... 我会试着改写我的问题......

Is there a way to pass arguments securly to another process so only the other process will be able to read the arguments. 有没有办法将参数安全地传递给另一个进程,因此只有其他进程才能读取参数。

It works but on the downside is that it also shows a second command prompt, the enviroment vars are only set in the context of the started process so the settings are not propagating to the whole box. 它有效,但缺点是它还显示第二个命令提示符,环境变量仅在启动过程的上下文中设置,因此设置不会传播到整个框。

    static void Main(string[] args)
    {
        var command = "cmd.exe";
        var environmentVariables = new System.Collections.Hashtable();
        environmentVariables.Add("some", "value");
        environmentVariables.Add("someother", "value");

        var filename = Path.GetTempFileName() + ".cmd";
        StreamWriter sw = new StreamWriter(filename);
        sw.WriteLine("@echo off");
        foreach (DictionaryEntry entry in environmentVariables)
        {
            sw.WriteLine("set {0}={1}", entry.Key, entry.Value);
        } 
        sw.WriteLine("start /w {0}", command);
        sw.Close();
        var psi = new ProcessStartInfo(filename) {
            UseShellExecute = true, 
            Verb="runas"
        };
        var ps =  Process.Start(psi);
        ps.WaitForExit();
        File.Delete(filename);
    }

有一个更好的答案:您仍然可以使用具有UseShellExecute = true的ProcessStartInfo调用Process.Start(),前提是您调用它的方法已使用[STAThread]属性标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何安全地附加到 ProcessStartInfo.EnvironmentVariables["PATH"] - How to safely append to ProcessStartInfo.EnvironmentVariables["PATH"] ProcessStartInfo 动词 runas 不起作用 - ProcessStartInfo Verb runas not working ProcessStartInfo动词运行方式? - ProcessStartInfo Verb runas? ProcessStartInfo.EnvironmentVariables 和 ProcessStartInfo.Environment 有什么区别 - What is the difference between ProcessStartInfo.EnvironmentVariables and ProcessStartInfo.Environment 动词=“ runas”未以提升的方式运行 - Verb = “runas” not run as elevated ProcessStartInfo不包含“动词”的定义 - ProcessStartInfo does not contain a definition for 'Verb' 使用ShellExecuteEx和动词“ runas”运行应用程序时如何预选管理员? (Windows XP) - How to preselect Administrator when running an application using ShellExecuteEx with verb “runas”? (Windows XP) 当从使用更安全令牌创建的进程调用时,RunAs Verb 不显示 UAC 弹出窗口且不提升 - RunAs Verb not showing a UAC popup and not elevating, when invoked from a process created with a Safer token WiX CustomAction 的子进程忽略 -Verb runAs - Child process of WiX CustomAction ignores -Verb runAs 我们什么时候需要将 ProcessStartInfo.UseShellExecute 设置为 True? - When do we need to set ProcessStartInfo.UseShellExecute to True?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM