我在执行外部命令时使用的CMD,但是为什么ExitCode总是不为0呢?
ExitCode总是为1或是其它值(1为不成功,成功为0),有没有办法解决?
代码如下:
System.Diagnostics.Process p=new System.Diagnostics.Process();
p.StartInfo.FileName="cmd.exe";
p.StartInfo.UseShellExecute=false;
p.StartInfo.RedirectStandardInput=true;
p.StartInfo.RedirectStandardOutput=true;
p.Start();
p.StandardInput.WriteLine(@" cd  "+basepath);  
string disk=basepath.Substring(0,2);
p.StandardInput.WriteLine("" +disk+ "");
string cmd=" xxxx -u "+UserName+" -p "+Password+"  "+TestCenter+"  -f "+path;
p.StandardInput.WriteLine(cmd);
}p.StandardInput.WriteLine("exit");
p.WaitForExit();
if((p.ExitCode==0)) {
    returnvalue=true;
}

解决方案 »

  1.   

    '1为不成功,成功为0',没这么一说吧
    The code that the associated process specified when it terminated.
      

  2.   

    可执行程序在完成运行时设置退出代码。它在过程结束时传递状态信息。通常,它用于将错误代码(或某些其他信息段)发送回调用方。如果过程仍未完成,ExitCode 属性返回 0。ExitCode 返回的值取决于所调用的应用程序。
    开发人员通常使用为零的 ExitCode 值指示成功退出,并通过非零值指出错误,调用方法可使用非零值来标识进程不正常终止的原因。虽然不必遵从这些准则,但这些准则是约定。
      

  3.   

    楼上的,也就是说不一定ExitCode 必须是0才表示成功吗?