C#调用cmd并实时返回数据方法,例如ping时,如图,
this.tabControl1.SelectedTab = tabPage3; Process proc = new Process(); proc.StartInfo.FileName = "cmd.exe"; //proc.StartInfo.WorkingDirectory = proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.RedirectStandardInput = true; proc.Start(); proc.StandardInput.WriteLine("tcping " + pingstr + " 80"); proc.StandardInput.WriteLine("exit"); StreamReader reader = proc.StandardOutput; string line = reader.ReadLine(); while (!reader.EndOfStream) { this.textBox2.AppendText(line + "\r\n"); this.textBox2.SelectionStart = this.textBox2.Text.Length; this.textBox2.ScrollToCaret(); line = reader.ReadLine(); } proc.WaitForExit(); proc.Close(); reader.Close();
亲爱的:若该文章解决了您的问题,可否收藏+评论+分享呢?
文章评论 本文章有个评论