Start command line with parameters and collection

2019-08-18 06:54发布

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. 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.

  2. When running it this way I get :

    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

  3. 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 ) .

2条回答
Melony?
2楼-- · 2019-08-18 07:13

Managed to do it in the End with .

        richTextBox1.Text = "";
        int counter = 0 ,totalMemory=0;
        string line;
        string command = " /c ro -a -h " + textBox1.Text + " -sn HC* $$info Server Total = $servermemory Service Memory = $servicememory > c:\\noc\\ntb\\logs\\output.txt";
        ProcessStartInfo procStartInfo = new ProcessStartInfo("CMD", command);
        Process proc = new Process();
        procStartInfo.CreateNoWindow = true;
        procStartInfo.RedirectStandardOutput = true;
        procStartInfo.RedirectStandardError = true;
        procStartInfo.UseShellExecute = false;
        proc.StartInfo = procStartInfo;
        proc.Start();
        proc.WaitForExit();
查看更多
Ridiculous、
3楼-- · 2019-08-18 07:26

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";
查看更多
登录 后发表回答