日期:2014-05-18 浏览次数:20973 次
if (p.HasExited) { //从输出流获取执行结果 strRst = p.StandardOutput.ReadToEnd(); }
------解决方案--------------------
static void Main(string[] args) { Process ps = new Process(); ps.StartInfo.FileName = "cmd.exe"; ps.StartInfo.RedirectStandardOutput = true; ps.StartInfo.RedirectStandardInput = true; ps.StartInfo.CreateNoWindow = true; ps.StartInfo.UseShellExecute = false; ps.StartInfo.StandardOutputEncoding = System.Text.Encoding.Default; ps.StartInfo.RedirectStandardError = false; ps.OutputDataReceived += new DataReceivedEventHandler(ps_OutputDataReceived); ps.Start(); ps.BeginOutputReadLine(); ps.StandardInput.WriteLine(@"dir c:\"); ps.StandardInput.Close(); ps.WaitForExit(); ps.Close(); } static void ps_OutputDataReceived(object sender, DataReceivedEventArgs e) { if (e.Data == null) { return; } Console.Write(e.Data+"\n"); }
------解决方案--------------------