C# 调用外部exe程序,出现已停止工作

1.在一个项目中,一个Library 调用另外一个Library的可执行文件时,如果用Process.Start(exe文件)(如果该exe文件没有相关的配置文件,则可以执行成功),但是如果有相关的配置文件,则会出现该exe文件已停止工作的错误提示。出现这样的原因是这样的,本身调用的时候,目录是自己bin/debug文件夹的路径,之后你设置其他路径的时候,就会出现错误,可以用cmd调试测试下,直接打开cmd,将exe要调用的程序放入cmd中执行,看是否出错,如果不出错,那么使用Process.Start("exe文件")调用也没问题的,如果出错,则是路径的问题。

解决方法:程序操作cmd来调用exe程序,先cd到exe程序的所在目录 

代码如下:

          Process p = new Process();//新进程
            p.StartInfo.FileName = "cmd.exe";//打开cmd程序
            p.StartInfo.UseShellExecute = false;//不使用shell启动程序
           p.StartInfo.RedirectStandardInput = true;
          p.StartInfo.RedirectStandardOutput = true;
          p.StartInfo.RedirectStandardError = true;
          p.StartInfo.CreateNoWindow = true;//true表示不显示黑框,false表示显示dos界面

            try
          {
              p.Start();//启动

                p.StandardInput.WriteLine(@"cd");
              p.StandardInput.WriteLine(@"cd " + AppDomain.CurrentDomain.BaseDirectory + "ProWin" + "");//执行程序所在目录
                p.StandardInput.WriteLine(@"" + AppDomain.CurrentDomain.BaseDirectory + @"ProWinWindowsUI.exe" + "");//执行程序具体位置

                p.StandardInput.WriteLine("exit");//退出
                p.Close();//关闭
            }
            catch (Exception ex)
            {

            }
原文地址:https://www.cnblogs.com/ybb521/p/3423463.html