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);
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 .
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.
Use the ProcessStartInfo.WorkingDirectory property.
Docs here.
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.
Use the ProcessStartInfo
class and assign a value to the WorkingDirectory
property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With