繁体   English   中英

C#OutputDataReceived标记为仅显示最后一行

[英]C# OutputDataReceived to label only showing last line

C#的新手,所以这可能有一个明显的答案,但是现在我被困住了。 我正在尝试运行基本命令,并将结果输出到标签或文本框。 代码如下:

    protected void Button3_Click(object sender, EventArgs e)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c " + "ping google.com")
        {
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        };

        Process process = Process.Start(startInfo);
        process.OutputDataReceived += (s, a) => 
        {
            if (!String.IsNullOrEmpty(a.Data))
            {
                Response.Write(a.Data + "<br />");
                Label1.Text = a.Data + Environment.NewLine;
            }
        };
        process.BeginOutputReadLine();
        process.WaitForExit();
    }

Response.Write输出显示的内容与我期望的一样,但是Label1.Text输出仅显示最后一行。 如何获取标签文本以显示命令的完整输出? 任何帮助,将不胜感激。

Label.Text = a.Data + Environment.NewLine; 每次调用OutputDataRecieved都重新分配Label.Text 如果要附加到它,解决方案将是:

Label.Text += a.Data + Environment.NewLine;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM