139

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?

7 Answers 7

217

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);
1
  • 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
    Commented Feb 12, 2018 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 .

2
  • 1
    Nice, I didn't know you could use environment variables in the .WorkingDirectory property.
    – Brain2000
    Commented Sep 13, 2011 at 14:39
  • 1
    Bingo, that's the answer -- the default directory is SYSTEMROOT. +1
    – ashes999
    Commented Oct 12, 2011 at 14:05
30

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.

2
  • 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 you will be able to comment on any post. Commented Apr 4, 2014 at 20:26
  • 12
    Even 6 years later, this comment about UseShellExecute was helpful. Fixed the issue.
    – AlanC
    Commented May 24, 2020 at 22:26
11

Use the ProcessStartInfo.WorkingDirectory property.

Docs here.

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.

4

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

0

I was in a catch 22 like @cbbspike as I wanted to have both UseShellExecute equal to false and set the WorkingDirectory to where the EXE should start. So I took @larry-smithmier answer and set the Environment.CurrentDirectory as he suggested.

Note: I did not have to surround the directory in quotes. In my case the scriptsPath variable was the directory C:\Program Files\SomeDir\


Environment.CurrentDirectory = scriptsPath;

ProcessStartInfo processStartInfo = new ProcessStartInfo("powershell.exe", scriptArguments);
processStartInfo.UseShellExecute = false;
//processStartInfo.WorkingDirectory = // intended working directory

Not the answer you're looking for? Browse other questions tagged or ask your own question.