繁体   English   中英

使用C#调用外部.exe时出现问题

[英]Problem invoking external .exe using c#

我正在尝试从我的C#代码运行此.exe文件,它确实调用了.exe文件,但随后中途崩溃了。 如果我在资源管理器上单击.exe,它将完成其工作,所以我想知道我用来调用它的代码是否存在问题:

            string fileName =  "loadscript.exe";
            Utils.Logger.Info("Calling script:" + fileName);
            Process process = new Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.FileName = fileName;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
            Thread.Sleep(10000);
            process.WaitForExit();
            int exitCode = process.ExitCode;
            string output = process.StandardOutput.ReadToEnd();
            Utils.Logger.Info(".exe Output: ");
            Utils.Logger.Info(output);
  Thread.Sleep(10000);
  process.WaitForExit();
  int exitCode = process.ExitCode;
  string output = process.StandardOutput.ReadToEnd();

在我看来,这似乎造成了僵局,这可能是最终崩溃的问题。 消除睡眠,然后尝试以下操作:

  string output = process.StandardOutput.ReadToEnd();
  process.WaitForExit();
  int exitCode = process.ExitCode;

请查看此问题的答案以获取解释:

重定向输出时ResGen.exe卡住

       process.StartInfo.UseShellExecute = false;

这要求您指定.exe文件的名称。 将其设置为true时,将使用另一个Windows函数来启动该文件,这一点足够聪明,可以弄清楚.bat文件需要启动cmd.exe来解释.bat文件中的命令。

这就是您现在需要做的事情,FileName必须为“ cmd.exe”,Arguments属性必须为“ loadscript.bat”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM