[英]Start command line with parameters and collection output
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();
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. 我需要在cmd
中运行一个命令,该命令将使用2个参数,这些参数将插入到textBox1和textBox2中,然后将输出发送到richTextBox1。
When running it this way I get : 以这种方式运行时,我得到:
first chance exception of type 'System.InvalidOperationException' occurred in System.dll System.dll中发生类型为'System.InvalidOperationException'的第一次机会异常
Additional information: StandardOut has not been redirected or the process hasn't started yet 附加信息:StandardOut尚未重定向或该过程尚未开始
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 ) . 我试图排除输出行,当我这样做时,它确实运行CMD,但没有键入任何命令(它只是打开CMD窗口,什么也不做)。
Process.Start()
asynchronously starts a new process. Process.Start()
异步启动一个新进程。 When you get to the process.StandardOutput.ReadToEnd()
the process is not finished yet, thus the exception. 当您进入process.StandardOutput.ReadToEnd()
该过程尚未完成,因此是异常。 You should use eventing to hook into the OutputDataRecieved event. 您应该使用事件挂钩到OutputDataRecieved事件。 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";
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.