Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Process.Start(ProcessStartInfo) fail?

Tags:

c#

process

We've developed a new WPF application, and I have had difficulty launching it from external C# script.

While calling Process.Start(ProcessStartInfo) method with ProcessStartInfo object which is initialized with both WorkingDirectory and FileName successes, init the FileName property only fails to launch.

This is not the case when calling any other application.
My question - Does different approach to start process has different logic?

See code for more details:

 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); }`

This is the image as it crashes: enter image description here

And following is the the problem signature from the crash screenshot:

 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
like image 812
Roi Shabtai Avatar asked Dec 15 '22 08:12

Roi Shabtai


1 Answers

When you do not specify the working directory, the new process will inherit the working directory of your process. That is, the new process will inherit the working directory of the process that called Process.Start().

That's the only difference between the two attempts to start MyApp. One of them inherits the working directory, and one of them specifies it. Clearly MyApp does not like to run with the initial working directory being the directory of your parent process.

Why that is so, I cannot say for sure. It would seem that MyApp is attempting some XML parsing at startup. So perhaps that XML parsing reads a file that is presumed to be in the working directory. But in fact the file is located in the same directory as the executable.

If that is the case then you need to modify MyApp to solve the problem. Instead of using a relative path for this XML file, you need to build an absolute path based on the directory of the executable file.

The MyApp startup code can that directory, like this:

string ExeDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.
    GetExecutingAssembly().Location));

And then you would use Path.Combine to form the full path to the XML file.

like image 143
David Heffernan Avatar answered Dec 18 '22 11:12

David Heffernan