#region 一个调用外部程序的例子
  private static string CmdPing(string strIp)
  &leftsign;
   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;
  &rightsign;
#endregion

习惯改为;private static string Cmd(string strcmd)
&leftsign;
 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.yippeesoft.com
 string strRst = p.StandardOutput.ReadToEnd();
 System.Diagnostics.Trace.WriteLine(strRst);
 p.Close();
 return strRst;
&rightsign;www.yippeesoft.com

结果:TestCase \’M:cvsaspx.CVS.test1\’ failed: 系统找不到指定的文件。www.yippeesoft.com
 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.yippeesoft.com

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.yippeesoft.com

Ping statistics for 127.0.0.1:www.yippeesoft.com
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0mswww.yippeesoft.com

历史博文

标签:, , , ,
七月 7, 2005 at 3:26 下午 by yippee 1,090 次
Category: Dev
Tags: , , , ,