user8755781
user8755781

Reputation:

Change the working directory of a running process with C#

I do not know if this is even possible without breaking/crashing the process but is there a way to change the working directory of a System.Diagnostics.Process like you would when executing the cd (change directory) command from the cmd.exe command line interface?

Upvotes: 6

Views: 15127

Answers (2)

Alex Seleznyov
Alex Seleznyov

Reputation: 945

As per MSDN, there is only one function which can change current folder, SetCurrentDirectory and it has single string parameter, so the change is for current process only.

            String oldWorkingDir = Directory.GetCurrentDirectory();
            Directory.SetCurrentDirectory(myDirInfo.FullName);
            operation();
            Directory.SetCurrentDirectory(oldWorkingDir);

ref

Upvotes: 6

microvio
microvio

Reputation: 91

You may set the working directory of the process with

myProcess.StartInfo.WorkingDirectory = "dir". 

Documentation here.

Upvotes: 9

Related Questions