Roi Shabtai
Roi Shabtai

Reputation: 3075

Why does Process.Start(ProcessStartInfo) fail?

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

Upvotes: 4

Views: 6026

Answers (2)

Scott
Scott

Reputation: 21501

When you don't provide the working directory it will use that of your current application.

Applications such as calc don't have any external file dependencies, so they don't care where they are launched from. They don't need to read any files from the working directory.

Your MyApp.exe most likely requires data from it's own working directory, possibly a config file. This test passes because it knows to look in C:\Program Files\MyAppFolder

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

Process.Start(pStartInfo2);

When you don't specify the working directory your app crashed because it can't load the required resource because it is trying to find it in the directory of you launching app.

It's always best to provide the working directory when launching an application, if you know it.

If you can update the MyApp.exe it can use System.Reflection.Assembly.GetExecutingAssembly().Location to determine it's own location, then you can read your file paths relative to this, thus eliminating the need for setting the working directory.

Upvotes: 6

David Heffernan
David Heffernan

Reputation: 612784

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.

Upvotes: 7

Related Questions