134

I'm firing off a Java application from inside of a C# .NET console application. It works fine for the case where the Java application doesn't care what the "default" directory is, but fails for a Java application that only searches the current directory for support files.

Is there a process parameter that can be set to specify the default directory that a process is started in?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian Knoblauch
  • 20,639
  • 15
  • 57
  • 92

6 Answers6

210

Yes! ProcessStartInfo Has a property called WorkingDirectory, just use:

...
using System.Diagnostics;
...

var startInfo = new ProcessStartInfo();

  startInfo.WorkingDirectory = // working directory
  // set additional properties 

Process proc = Process.Start(startInfo);
umlcat
  • 4,091
  • 3
  • 19
  • 29
Dror Helper
  • 30,292
  • 15
  • 80
  • 129
  • 1
    Can the `WorkingDirectory` be a relative path, and if so, what is it relative to, the current file or the directory of the current executable? I'm having trouble getting my program to find the executable I'm trying to start. I've set the working directory to the directory of the exe (relative to the current file and to the current exe), but it can't find it. – pushkin Feb 12 '18 at 21:31
66

Use the ProcessStartInfo.WorkingDirectory property to set it prior to starting the process. If the property is not set, the default working directory is %SYSTEMROOT%\system32.

You can determine the value of %SYSTEMROOT% by using:

string _systemRoot = Environment.GetEnvironmentVariable("SYSTEMROOT");  

Here is some sample code that opens Notepad.exe with a working directory of %ProgramFiles%:

...
using System.Diagnostics;
...

ProcessStartInfo _processStartInfo = new ProcessStartInfo();
  _processStartInfo.WorkingDirectory = @"%ProgramFiles%";
  _processStartInfo.FileName         = @"Notepad.exe";
  _processStartInfo.Arguments        = "test.txt";
  _processStartInfo.CreateNoWindow   = true;
Process myProcess = Process.Start(_processStartInfo);

There is also an Environment variable that controls the current working directory for your process that you can access directly through the Environment.CurrentDirectory property .

Larry Smithmier
  • 2,711
  • 2
  • 23
  • 30
29

Just a note after hitting my head trying to implement this. Setting the WorkingDirectory value does not work if you have "UseShellExecute" set to false.

CBBSpike
  • 1,385
  • 1
  • 13
  • 18
  • 2
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Cyclonecode Apr 04 '14 at 20:26
  • 11
    Even 6 years later, this comment about UseShellExecute was helpful. Fixed the issue. – AlanC May 24 '20 at 22:26
11

Use the ProcessStartInfo.WorkingDirectory property.

Docs here.

Ben Hoffstein
  • 102,129
  • 8
  • 104
  • 120
7

The Process.Start method has an overload that takes an instance of ProcessStartInfo. This class has a property called "WorkingDirectory".

Set that property to the folder you want to use and that should make it start up in the correct folder.

Simon Johnson
  • 7,802
  • 5
  • 37
  • 49
4

Use the ProcessStartInfo class and assign a value to the WorkingDirectory property.

Joseph Daigle
  • 47,650
  • 10
  • 49
  • 73