为什么Process.Start(ProcessStartInfo)失败?

Sat*_*ies 3 c# process

我们开发了一个新的WPF应用程序,我很难从外部C#脚本启动它.

在调用Process.Start(ProcessStartInfo)带有ProcessStartInfo对象WorkingDirectoryFileName成功初始化对象的方法时,init FileName属性只能无法启动.

调用任何其他应用程序时不是这种情况.
我的问题 - 启动过程的不同方法是否有不同的逻辑?

有关详细信息,请参阅代码

 public void LaunchApp(){
/********************************/
/*      This code PASSES        */
/********************************/
var pStartInfoCalc1 = new ProcessStartInfo
    {
        FileName = @"C:\Windows\system32\calc.exe",
    };

Process.Start(pStartInfoCalc1);

/*****************************/
/*  !!!This code FAILS  !!! */
/*****************************/
var pStartInfo1 = new ProcessStartInfo
    {
        FileName = @"C:\Program Files\MyAppFolder\MyApp.exe",
    };

Process.Start(pStartInfo1);

/********************************/
/*      This code PASSES        */
/********************************/
var pStartInfo2 = new ProcessStartInfo
    {
        WorkingDirectory = @"C:\Program Files\MyAppFolder",
        FileName = @"MyApp.exe",
    };

Process.Start(pStartInfo2);

/********************************/
/*      This code PASSES        */
/********************************/
var pStartInfoCalc2 = new ProcessStartInfo
    {
        WorkingDirectory = @"C:\Windows\system32\",
        FileName = @"calc.exe",
    };

Process.Start(pStartInfoCalc2); }`
Run Code Online (Sandbox Code Playgroud)

这是崩溃的图像: 在此输入图像描述

以下是崩溃屏幕截图中的问题签名:

 Problem signature:
  Problem Event Name:   CLR20r3
  Problem Signature 01: MyApp.exe
  Problem Signature 02: 1.0.0.0
  Problem Signature 03: 51ef9fd8
  Problem Signature 04: mscorlib
  Problem Signature 05: 4.0.30319.18052
  Problem Signature 06: 5173bf28
  Problem Signature 07: 266d
  Problem Signature 08: a4
  Problem Signature 09: System.Windows.Markup.XamlParse
  OS Version:   6.1.7601.2.1.0.256.4
  Locale ID:    1033
  Additional Information 1: 1989
  Additional Information 2: 1989c043e2e04efdbf18835c58bb867b
  Additional Information 3: 37d3
  Additional Information 4: 37d31c18f56cf3083b1c45ca83bbb78e

Read our privacy statement online:
  http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

If the online privacy statement is not available, please read our privacy statement offline:
  C:\Windows\system32\en-US\erofflps.txt
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 6

如果未指定工作目录,则新进程将继承进程的工作目录.也就是说,新进程将继承被调用进程的工作目录Process.Start().

这是两次尝试开始之间的唯一区别MyApp.其中一个继承了工作目录,其中一个指定了它.显然MyApp不喜欢在初始工作目录是父进程的目录的情况下运行.

为什么会这样,我不能肯定地说.看起来似乎MyApp是在启动时尝试一些XML解析.因此,XML解析可能会读取一个假定在工作目录中的文件.但实际上该文件与可执行文件位于同一目录中.

如果是这种情况,那么您需要修改MyApp以解决问题.您需要根据可执行文件的目录构建绝对路径,而不是使用此XML文件的相对路径.

MyApp启动代码可以在目录中,像这样的:

string ExeDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.
    GetExecutingAssembly().Location));
Run Code Online (Sandbox Code Playgroud)

然后,您将使用Path.Combine形成XML文件的完整路径.


Sco*_*ott 5

如果您不提供工作目录,它将使用您当前应用程序的目录.

诸如之类的应用程序calc没有任何外部文件依赖性,因此它们不关心它们从何处启动.他们不需要从工作目录中读取任何文件.

MyApp.exe最有可能需要来自其自己的工作目录的数据,可能是配置文件.这个测试通过,因为它知道要查看C:\Program Files\MyAppFolder

/********************************/
/*      This code PASSES        */
/********************************/
var pStartInfo2 = new ProcessStartInfo
{
    WorkingDirectory = @"C:\Program Files\MyAppFolder",
    FileName = @"MyApp.exe",
};

Process.Start(pStartInfo2);
Run Code Online (Sandbox Code Playgroud)

当您没有指定工作目录时,您的应用程序崩溃了,因为它无法加载所需的资源,因为它试图在您启动应用程序的目录中找到它.

如果您知道的话,最好在启动应用程序时提供工作目录.

如果您可以更新MyApp.exe它可以System.Reflection.Assembly.GetExecutingAssembly().Location用来确定它自己的位置,那么您可以读取相对于此的文件路径,从而无需设置工作目录.