在 C# 中将命令发送到 cmd 提示符 [英] Sending commands to cmd prompt in C#

查看:32
本文介绍了在 C# 中将命令发送到 cmd 提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的一个实现,我正在开发一个工具,该工具应该向/从 cmd 窗口发送/检索命令/结果.一切正常,但下面的用例无法执行任何操作.好像我的应用程序正在等待某些东西(而不是显示结果)

For one of my implementations I am working on a tool that is supposed to send/retrieve commands/results to/from the cmd window. Everything works fine but the Use case below fails to do anything. It seems as if my application is waiting for something (instead of displaying the result)

从我的工具导航到 python 文件夹.我尝试从 python 文件夹启动 python.exe,但此时,我的编辑器没有做任何事情.它只是继续等待.

From my tool I navigate to the python folder . From the python folder I try to launch python.exe but at this point, my editor does not do anything. it simply keeps on waiting.

出于您的考虑,我也在此处链接了视频.你们更容易理解我想说什么.

For your kind consideration I am also linking the video here. It would be easier for you guys to understand what I am trying to say.

在此处(在 youtube 上)观看视频

我还附上了我目前拥有的代码.

I am also attaching the code that I currently have.

            ProcessStartInfo info = new ProcessStartInfo("cmd.exe");

            string argument = null;
            if (!string.IsNullOrEmpty(startingDirectory) && System.IO.Directory.Exists(startingDirectory))
            {
               argument += @"cd";
            }
            else
            {
                argument += """;
            }
            info.Arguments = argument;
            info.CreateNoWindow = true;
            info.RedirectStandardError = true;
            info.RedirectStandardInput = true;
            info.RedirectStandardOutput = true;
            info.UseShellExecute = false;
             this.shellProcess = System.Diagnostics.Process.Start(info);
            this.shellProcess.EnableRaisingEvents = true;
            //this.InputStream.AutoFlush = true;
            this.shellProcess.Exited += new EventHandler(ProcessExited);
            this.ErrorBeginRead();
            this.OutputBeginRead();

 private void OutputBeginRead()
    {
        this.shellProcess.StandardOutput.BaseStream.BeginRead(outputBuffer, 0, outputBuffer.Length, new AsyncCallback(this.OnOutputInput), this.shellProcess);
    }

        private void ErrorBeginRead()
    {
        this.shellProcess.StandardError.BaseStream.BeginRead(errorBuffer, 0, errorBuffer.Length, new AsyncCallback(this.OnErrorInput), this.shellProcess);
    }

谢谢!

启动 python 只是一个例子.我也需要对其他普通的 cmd 行命令使用相同的方法.如果有人能指出我在我拥有的代码中做错了什么或我必须做什么,以实现预期的功能,那就太好了.

Launching python is just an example. I need to use the same method for other normal cmd line commands as well.It would be nice, if somebody can point what i am doing wrong with the code that I have or what I must do , in order to achieve the intended functionality.

EDIT 2:正常的 cmd 命令工作正常.python、perl 等命令行工具不起作用.

EDIT 2 : The normal cmd commands are working perfectly. The command line tools like python,perl are not working .

编辑 3:所以我设法按照杰米的建议向前推进了一点.用户界面不再挂起"了.但是当我访问 python 解释器时,解释器的输出在我的工具中仍然不可见.任何建议为什么会发生这种情况?

Edit 3 : So I managed to do move a wee bit forward following Jamie's suggestions. The ui is not "hanging" anymore. but when i access the python interpreter , the interpreter's output is still not visible in my tool. Any suggestions why that might be happening ?

推荐答案

您无法通过这种方式向 shell 发送命令.info.Arguments 中的字符串是在命令行上提供给程序的参数.如果您希望 cmd.exe shell 执行一系列命令然后退出,则必须提供/c 参数.如果您希望它执行多个命令,则必须将这些命令放入批处理文件中并执行该命令,或者将它们括在引号中并用 && 分隔它们,即 info.Arguments = @"/c ""cd && dir""";.永不返回的另一个问题是 cmd.exe 在没有任何或适当的参数的情况下执行时默认以交互模式打开./c 选项告诉 cmd.exe 执行相关命令然后退出.

You cannot send commands to a shell this way. The string in info.Arguments is the arguments provided to the program on the command line. If you want the cmd.exe shell to execute a series of command and then quit you will have to provide the /c argument. If you have multiple commands that you want it to perform you will either have to put the commands in a batch file and execute that or enclose them in quotes and separate them with &&, i.e. info.Arguments = @"/c ""cd && dir""";. Your other issue with never returning is that cmd.exe opens in interactive mode by default when it is executed without any, or proper, arguments. The /c option tells cmd.exe to execute the relevant commands and then quit.

此外,当直接从 ProcessStartInfo 启动时,像 python 和 perl 这样的解释器有时会出现奇怪的行为.如果带有 perl.exe 的 info.Arguments = @"""MyPerlProgram.pl"""; 不起作用,您可能会发现有必要在 cmd.exe 中启动它们以获得正常行为它们,即 info.Arguments = @"/c ""perl.exe ""MyPerlProgram.pl""""";.

Additionally, interpreters like python and perl sometimes have weird behaviors when launched directly from ProcessStartInfo. If info.Arguments = @"""MyPerlProgram.pl"""; with perl.exe doesn't work, you may find it necessary to launch them inside cmd.exe to get normal behavior out of them, i.e. info.Arguments = @"/c ""perl.exe ""MyPerlProgram.pl""""";.

参见 CmdProcessStartInfo.Arguments 属性.

要回答您的编辑 3 问题,您可能没有正确地连接到输出.与其尝试挂钩 StreamReader 的 BaseStream,不如在调用 Start 之前使用 this.shellProcess.OutputDataReceived += ProcessOutputHandler; 挂钩 OutputDataReceived 事件,其中 ProcessOutputHandler 具有类似 public static void ProcessOutputHandler(object sentProcess, DataReceivedEventArgs outLine).调用 Start 后立即调用 this.shellProcess.BeginOutputReadLine();.错误输出的过程也类似.请参阅 Process.BeginOutputReadLine 方法Process.BeginErrorReadLine 方法 了解更多详情.

To answer your Edit 3 problem, you're probably not correctly hooking into the outputs. Instead of trying to hook the StreamReader's BaseStream, hook the OutputDataReceived event with this.shellProcess.OutputDataReceived += ProcessOutputHandler; before you call Start where ProcessOutputHandler has a signature like public static void ProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine). Immediately after calling Start, call this.shellProcess.BeginOutputReadLine();. The process is similar for the error ouput as well. See Process.BeginOutputReadLine Method and Process.BeginErrorReadLine Method for more details.

如果你仍然有问题,你只要试试 process.StartInfo.Arguments = @"/c ""python.exe -c ""import sys; print 'Test.';""""";?

If you still have a problem, what do you get if you just try process.StartInfo.Arguments = @"/c ""python.exe -c ""import sys; print 'Test.';""""";?

此外,下面的代码演示了 shell 通信的大部分必要概念:

Also, the code below demonstrates most of the necessary concepts for shell communication:

public static void Main()
{
    using (Process process = new Process())
    {
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.WorkingDirectory = @"C:";
        process.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");

        // Redirects the standard input so that commands can be sent to the shell.
        process.StartInfo.RedirectStandardInput = true;
        // Runs the specified command and exits the shell immediately.
        //process.StartInfo.Arguments = @"/c ""dir""";

        process.OutputDataReceived += ProcessOutputDataHandler;
        process.ErrorDataReceived += ProcessErrorDataHandler;

        process.Start();
        process.BeginOutputReadLine();
        process.BeginErrorReadLine();

        // Send a directory command and an exit command to the shell
        process.StandardInput.WriteLine("dir");
        process.StandardInput.WriteLine("exit");

        process.WaitForExit();
    }
}

public static void ProcessOutputDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
    Console.WriteLine(outLine.Data);
}

public static void ProcessErrorDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
    Console.WriteLine(outLine.Data);
}

您可能有线程问题导致您的问题.我对此做了一些进一步的工作,并且能够在表单上获取一个文本框以使用以下代码进行更新:

You may have threading issues causing your problems. I've done some further work with this and was able to get a textbox on a form to update with the following code:

using System;
using System.Diagnostics;
using System.IO;
using System.Timers;

namespace DummyFormsApplication
{
    class ProcessLauncher : IDisposable
    {
        private Form1 form;
        private Process process;
        private bool running;

        public bool InteractiveMode
        {
            get;
            private set;
        }

        public ProcessLauncher(Form1 form)
        {
            this.form = form;

            process = new Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.WorkingDirectory = @"C:";
            process.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");

            // Redirects the standard input so that commands can be sent to the shell.
            process.StartInfo.RedirectStandardInput = true;

            process.OutputDataReceived +=new DataReceivedEventHandler(process_OutputDataReceived);
            process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived);
            process.Exited += new EventHandler(process_Exited);
        }

        public void Start()
        {
            if (running == false)
            {
                running = true;
                InteractiveMode = true;

                // Runs the specified command and exits the shell immediately upon completion.
                process.StartInfo.Arguments = @"/c ""C:python27python.exe -i""";

                process.Start();

                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
            }
        }

        public void Start(string scriptFileName)
        {
            if (running == false)
            {
                running = true;
                InteractiveMode = false;

                // Runs the specified command and exits the shell immediately upon completion.
                process.StartInfo.Arguments = string.Format(@"/c ""C:python27python.exe ""{0}""""", scriptFileName);
            }
        }

        public void Abort()
        {
            process.Kill();
        }

        public void SendInput(string input)
        {
            process.StandardInput.Write(input);
            process.StandardInput.Flush();
        }

        private void process_OutputDataReceived(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (outLine.Data != null)
            {
                form.Invoke(form.appendConsoleTextDelegate, new object[] { outLine.Data });
            }
        }

        private void process_ErrorDataReceived(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (outLine.Data != null)
            {
                form.Invoke(form.appendConsoleTextDelegate, new object[] { outLine.Data });
            }
        }

        private void process_Exited(object sender, EventArgs e)
        {
            running = false;
        }

        public void Dispose()
        {
            if (process != null)
            {
                process.Dispose();
            }
        }
    }
}

我创建了一个表单并在表单中添加了一个文本框和以下代码:

I created a form and added a textbox and the following code in the form:

    public delegate void AppendConsoleText(string text);
    public AppendConsoleText appendConsoleTextDelegate;

    private void Form1_Load(object sender, EventArgs e)
    {
        appendConsoleTextDelegate = new AppendConsoleText(textBox1_AppendConsoleText);
        using (ProcessLauncher launcher = new ProcessLauncher(this))
        {
            launcher.Start();

            launcher.SendInput("import sys;
");
            launcher.SendInput("print "Test.";
");
            launcher.SendInput("exit()
");
        }
    }

    private void textBox1_AppendConsoleText(string text)
    {
        textBox1.AppendText(string.Format("{0}
", text));
    }

需要注意的一点是,如果 Form1_Load 事件没有完成,Invoke 将挂起直到它完成.如果事件中有长时间运行的代码,则需要使用 BeginInvoke 异步调用,或者在长时间运行的代码中定期调用 DoEvents.

One thing to note is that if the Form1_Load event doesn't complete, Invoke will hang until it does. If you have long-running code in an event you'll either need to invoke asynchronously using BeginInvoke, or periodically call DoEvents in your long-running code.

编辑

根据您的评论,我修改了代码以处理交互式提交.然而,有一个问题.在 StandardError 输出中提供了 Python 提示 (>>>>),它不回显 StandardInput.它也不会终止该线路.这使得检测提示变得困难,并且由于 process_ErrorDataReceived 直到进程结束或看到行结束才会触发,从而导致提示字符的一些乱序输出.

Per your comment, I've modified the code to work with interactive submissions. There is, however, a problem. The python prompt (>>>) is provided on the StandardError output and it does not echo the StandardInput. It also does not terminate the line. This makes detecting a prompt difficult and causes some out of order output of the prompt characters due to the process_ErrorDataReceived not firing until either the process ends or a line end is seen.

这篇关于在 C# 中将命令发送到 cmd 提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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