如何在文本框中显示iperf cmd提示的输出

时间:2014-05-21 11:53:07

标签: c# command-prompt iperf

我正在使用iperf-2.0.5-2-win32工具来查找网络带宽。我在c#中编写代码,打开cmd提示符,传递iperf参数以启动服务器端&客户端。 iperf-2.0.5-2-win32 exe不会直接打开,只需要通过cmd提示打开。 目前输出(传输速率和带宽)显示在cmd提示符本身上。我希望这些输出显示在文本框中 我也尝试过StreamReader。但它需要null,我也尝试过OutputDataReceived Event,它也取空。 找到了很少的ipconfig和amp;代码ping。但那些没有使用iperf代码。

button_click event(),
{
Process Client_proc = new Process();
ProcessStartInfo Client_command = new ProcessStartInfo("cmd.exe"); 
string ip = txtIP.Text;
Client_command.CreateNoWindow = true;
Client_command.WindowStyle = ProcessWindowStyle.Hidden;
Client_command.WorkingDirectory = @"E:\Iperf\RunEXE_Through_Application\iperf-2.0.5-2-win32";
Client_command.Arguments = "/c START iperf -c " + ip;
Client_proc.StartInfo = Client_command;
Client_command.RedirectStandardOutput = true;
Client_command.UseShellExecute = false;
Client_proc.OutputDataReceived += new DataReceivedEventHandler(Client_proc_OutputDataReceived);
Client_proc.Start(); 
Client_proc.BeginOutputReadLine(); 
Client_proc.WaitForExit();
}

void Client_proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
string newLine = e.Data.Trim() + Environment.NewLine;
MethodInvoker append = () => txtOutput.Text += newLine;
txtOutput.BeginInvoke(append);
}
}

Plz帮助我。早期的回复表示赞赏 谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用此完整代码进行处理 它并不完美(使用多个流时会出现一些问题)

public void RunProcess(string FileName, string Arguments, bool EventWhenExit )
{
    process = new Process();

    process.EnableRaisingEvents = true;
    process.OutputDataReceived += new DataReceivedEventHandler(OnDataReceivedEvent);
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.LoadUserProfile = false;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.FileName = FileName; // Gets or sets the application or document to start.
    process.StartInfo.Arguments = Arguments;//Gets or sets the set of command-line arguments to use when starting the application      
    Thread.Sleep(1000);
    if (EventWhenExit)
    {
        process.EnableRaisingEvents = true;

        process.Exited += new EventHandler(myprocess_Exited);/*New line */

    }

    process.Start();
    process.BeginOutputReadLine();
    PID = process.Id;


}

private void myprocess_Exited(object sender, EventArgs e)
{
    process.Refresh();
    Thread.Sleep(2000);
    onProcessEnd(this, "ENDOF " + Proc.ToString());
    Console.WriteLine("Process exsiting ");
}


private void OnDataReceivedEvent(object sender, DataReceivedEventArgs e)
{

    string OutputFromProcess = e.Data;
    //fire event to event handler class for further use
    onDataOutputFromProcess(this, OutputFromProcess, Proc.ToString());
}

,而不是在GUI层中,您应该绑定到onDataOutputFromProcess事件 那里你应该有类似

的东西
if (screenToPrint.InvokeRequired) //&& this.Visible)
{
    try
    {
        this.Invoke(new Action<AppendToScreenParam>(AppendTextFullConfig), new object[] { append });
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    return;
}
else
{
    screenToPrint.SelectionFont = font;
    screenToPrint.SelectionColor = append.Color;
    //screenToPrint.AppendText(append.Message);
    string TextToPrint = string.Format("{0}\n", append.Message);
    screenToPrint.AppendText(TextToPrint);
}

}

答案 1 :(得分:0)

可能是因为iperf进程返回错误。使用ErrorDataReceived订阅Client_proc.ErrorDataReceived += Client_proc_ErrorDataReceived;活动并查看结果。如果命令返回错误,则可以将错误消息视为输出。

void Client_proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    if (e.Data != null)
    {
        this.txtOutput.BeginInvoke(new MethodInvoker(() => { this.txtOutput.Text = e.Data; }));
    }
}