.NET Process.Start default directory?

C#

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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 - C#

Use the [ProcessStartInfo.WorkingDirectory][1] 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][2] property .

[1]: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx "Link to MSDN documentation on ProcessStartInfo.WorkingDirectory" [2]: http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx "MSDN documentation on Environment.CurrentDirectory"

Solution 3 - C#

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.

Solution 4 - C#

Use the ProcessStartInfo.WorkingDirectory property.

Docs here.

Solution 5 - C#

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.

Solution 6 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian KnoblauchView Question on Stackoverflow
Solution 1 - C#Dror HelperView Answer on Stackoverflow
Solution 2 - C#Larry SmithmierView Answer on Stackoverflow
Solution 3 - C#CBBSpikeView Answer on Stackoverflow
Solution 4 - C#Ben HoffsteinView Answer on Stackoverflow
Solution 5 - C#Simon JohnsonView Answer on Stackoverflow
Solution 6 - C#Joseph DaigleView Answer on Stackoverflow