使用参数和收集输出启动命令行 [英] Start command line with parameters and collection output

查看:77
本文介绍了使用参数和收集输出启动命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ProcessStartInfo startInfo = new ProcessStartInfo();
//startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "tsvc -a -st rifs -h "+textBox1+" -sn "+textBox2+" -status";

Process process = new Process();
process.StartInfo = startInfo;
process.Start();

richTextBox1.Text = process.StandardOutput.ReadToEnd();




  1. 我需要在<$ c $中运行命令c> cmd 将带有2个参数,这些参数将插入到textBox1和textBox2中,然后将输出发送到richTextBox1。

  1. I need to run a command in cmd that will take 2 parameters that will be inserted to textBox1 and textBox2 and then sending the output to the richTextBox1.

当以这种方式运行时,我得到:

When running it this way I get :


类型为 System.InvalidOperationException的第一次机会异常发生在System.dll中

其他信息:StandardOut尚未重定向或进程尚未开始

first chance exception of type 'System.InvalidOperationException' occurred in System.dll
Additional information: StandardOut has not been redirected or the process hasn't started yet


  • 我试图排除输出行,当我这样做时,它确实运行CMD,但没有键入任何命令(它只是打开CMD窗口,什么也不做)。

  • I tried to exclude the Output line , and when i do it does run CMD but does not type in any command (It just opens a CMD window and does nothing ) .


    推荐答案

    Process.Start()异步启动一个新进程。当您进入 process.StandardOutput.ReadToEnd()时,该过程尚未完成,因此是异常。您应该使用事件挂钩到OutputDataRecieved事件。您想要执行以下操作:

    Process.Start() asynchronously starts a new process. When you get to the process.StandardOutput.ReadToEnd() the process is not finished yet, thus the exception. You should use eventing to hook into the OutputDataRecieved event. You want to do something like this:

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/c tsvc -a -st rifs -h " + textBox1 + " -sn " + textBox2 + " -status";
    
            process.StartInfo = startInfo;
            process.OutputDataReceived += ProcessOnOutputDataReceived;
            process.ErrorDataReceived += ProcessOnOutputDataReceived;
            process.Start();
    

    然后将事件处理程序添加到接收到的输出数据中,如下所示:

    and then add an event handler to the Output data recieved like so:

        private void ProcessOnOutputDataReceived(object sender, DataReceivedEventArgs dataReceivedEventArgs)
        {
            richTextBox1.Text += dataReceivedEventArgs.Data;
        }
    

    此外,我不确定,但我认为您需要在自己的电话上输入文字文本框:

    Also, I am not certain but i think you need to call text on your textboxes:

    startInfo.Arguments = "/c tsvc -a -st rifs -h " + textBox1.Text + " -sn " + textBox2.Text + " -status";
    

    这篇关于使用参数和收集输出启动命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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