#region 一个调用外部程序的例子 private static string CmdPing(string strIp) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; string pingrst; p.Start(); p.StandardInput.WriteLine("telnet 192.168.3.175 9944"); p.StandardInput.WriteLine("quit"); p.StandardInput.WriteLine("exit"); string strRst = p.StandardOutput.ReadToEnd(); Trace.WriteLine(strRst); p.Close(); return pingrst; } #endregion 习惯改为;private static string Cmd(string strcmd) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe /c"+strcmd; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start();www.shengfang.org string strRst = p.StandardOutput.ReadToEnd(); System.Diagnostics.Trace.WriteLine(strRst); p.Close(); return strRst; }www.shengfang.org 结果:TestCase 'M:cvsaspx.CVS.test1' failed: 系统找不到指定的文件。www.shengfang.org System.ComponentModel.Win32Exception: 系统找不到指定的文件。 at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() f:\inetpub\wwwroot\cvsaspx\cvs.aspx.cs(42,0): at cvsaspx.CVS.Cmd(String strcmd) f:\inetpub\wwwroot\cvsaspx\cvs.aspx.cs(30,0): at cvsaspx.CVS.test1() 真是失败。[hide] p.StartInfo.Arguments="/c "+strcmd; [/hide]www.shengfang.org Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128www.shengfang.org Ping statistics for 127.0.0.1:www.shengfang.org Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0mswww.shengfang.org 字体:大 中 小 |