本文介绍了以管理员身份运行 cmd 和命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

try
{
    ProcessStartInfo procStartInfo = new ProcessStartInfo(
                                            "cmd.exe",
                                            "/c " + command);
    procStartInfo.UseShellExecute = true;
    procStartInfo.CreateNoWindow = true;
    procStartInfo.Verb = "runas";
    procStartInfo.Arguments = "/env /user:" + "Administrator" + " cmd" + command;

    ///command contains the command to be executed in cmd
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

我想保留

procStartInfo.UseShellExecute = true
procStartInfo.RedirectStandardInput = false;

是否可以不使用process.standardinput 来执行命令?我尝试执行我在参数中传递的命令,但该命令没有执行.

Is it possible to execute the command without using process.standardinput?I try to execute command I've passed in argument but the command does not executes.

推荐答案

正如@mtijn 所说,你有很多事情要做,但你以后也会覆盖.您还需要确保正确转义.

As @mtijn said you've got a lot going on that you're also overriding later. You also need to make sure that you're escaping things correctly.

假设您要运行以下提升的命令:

Let's say that you want to run the following command elevated:

dir c:

首先,如果你只是通过 Process.Start() 运行这个命令,一个窗口会立即打开和关闭,因为没有什么可以让窗口保持打开状态.它处理命令并退出.为了保持窗口打开,我们可以将命令包装在单独的命令窗口中,并使用 /K 开关使其保持运行:

First, if you just ran this command through Process.Start() a window would pop open and close right away because there's nothing to keep the window open. It processes the command and exits. To keep the window open we can wrap the command in separate command window and use the /K switch to keep it running:

cmd /K "dir c:"

要运行提升的命令,我们可以像您一样使用 runas.exe ,但我们需要更多地转义.根据帮助文档 (runas/?),我们传递给 runas 的命令中的任何引号都需要使用反斜杠进行转义.不幸的是,使用上面的命令这样做会给我们一个双反斜杠,它混淆了 cmd 解析器,因此也需要转义.所以上面的命令最终将是:

To run that command elevated we can use runas.exe just as you were except that we need to escape things a little more. Per the help docs (runas /?) any quotes in the command that we pass to runas need to be escaped with a backslash. Unfortunately doing that with the above command gives us a double backslash that confused the cmd parser so that needs to be escaped, too. So the above command will end up being:

cmd /K "dir c:\"

最后,使用您提供的语法,我们可以将所有内容包装成一个 runas 命令,并将上面的命令括在另一组引号中:

Finally, using the syntax that you provided we can wrap everything up into a runas command and enclose our above command in a further set of quotes:

runas /env /user:Administrator "cmd /K "dir c:\""

从命令提示符运行上述命令以确保其按预期工作.

Run the above command from a command prompt to make sure that its working as expected.

鉴于所有这些,最终代码变得更容易组装:

Given all that the final code becomes easier to assemble:

        //Assuming that we want to run the following command:
        //dir c:

        //The command that we want to run
        string subCommand = @"dir";

        //The arguments to the command that we want to run
        string subCommandArgs = @"c:";

        //I am wrapping everything in a CMD /K command so that I can see the output and so that it stays up after executing
        //Note: arguments in the sub command need to have their backslashes escaped which is taken care of below
        string subCommandFinal = @"cmd /K """ + subCommand.Replace(@"", @"\") + " " + subCommandArgs.Replace(@"", @"\") + @"""";

        //Run the runas command directly
        ProcessStartInfo procStartInfo = new ProcessStartInfo("runas.exe");
        procStartInfo.UseShellExecute = true;
        procStartInfo.CreateNoWindow = true;

        //Create our arguments
        string finalArgs = @"/env /user:Administrator """ + subCommandFinal + @"""";
        procStartInfo.Arguments = finalArgs;

        //command contains the command to be executed in cmd
        using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
        {
            proc.StartInfo = procStartInfo;
            proc.Start();
        }

这篇关于以管理员身份运行 cmd 和命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 19:52
查看更多