.NET Process.Start default directory?

Solution 1:

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

Solution 2:

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 .

Solution 3:

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.