25

I have a project , running from c:\work\SomeVariantFolder\MySolution\MyProject\Bin\Debug, and I need to execute a command line from this project from one of the subfolders : c:\work\SomeVariantDev. The problem I am facing is to get from the folder where my project running from to the folder where i am suppose to run this command line from.

Please note that I can't use batch file for this solution.

What I've tried to do - declare a private method which execute three commands from the same process, going four folders up and then execute my command, but that doesn't seem to work. I feel like I'm doing something wrong here because if I run this command from c:\work\SomeVariantFolder\ it wroks well.

var process = new System.Diagnostics.Process();
var startInfo = new System.Diagnostics.ProcessStartInfo
                {
                       WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
                       FileName = "cmd.exe",
                       RedirectStandardInput = true,
                       UseShellExecute = false
                };

process.StartInfo = startInfo;
process.Start();



 process.StandardInput.WriteLine("cd..");
 process.StandardInput.WriteLine("cd..");
 process.StandardInput.WriteLine("cd..");
 process.StandardInput.WriteLine("cd..");

 process.StandardInput.WriteLine("my command");

Please note that due the nature of my solution I can't use batch files and can't use c:\work\SomeVariantFolder as a hard coded folder since "SomeVariantFolder" name may change under some circumstances.

Any help would be appriciated

Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21
user3150947
  • 343
  • 1
  • 3
  • 7
  • to ensure you are where you want to be you could first select the drive `C:` then go to the root of that drive `cd\\` then enter the directory you finally want `cd path\to\subfolder`; keep in mind that you need to either escape "\" in c#-strings or precede the string with `@` to ignore escape sequences – DrCopyPaste Jan 13 '14 at 08:27
  • 2
    Instead of `CD`-ing from one directory to another, why not set the `WorkingDirectory` of the `ProcessStartInfo` directly? – IronGeek Jan 13 '14 at 08:31
  • @NahumLitvin, I tried to use this example but it didn't setisfy my needs. Thomas W , This is a good answer which I didn't came across. IronGeek , I believe that this is the right answer, If you could answer my question I will mark you . – user3150947 Jan 13 '14 at 08:35
  • What IronGeek said. Also, don't forget that you may run into permission issues - since Windows loads DLLs in working directory with a high priority, you may be stopped from changing the working directory if you're not running the application with root permissions. Trying to run an application with root privileges is always a good try to catch a lot of permission issues :) – Luaan Jan 13 '14 at 08:38
  • @user3150947 I've posted an answer as per your request – IronGeek Jan 13 '14 at 08:44
  • The linked question does not explain how to set the working directory, thus this is not a duplicate. – Martin Braun Jan 13 '22 at 01:51

2 Answers2

35

Try setting the WorkingDirectory property of ProcessStartInfo to sets the initial directory for the process to be started.

var startInfo = new System.Diagnostics.ProcessStartInfo
{
  WorkingDirectory = @"The\Process\Working\Directory",
  WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal,
  FileName = "cmd.exe",
  RedirectStandardInput = true,
  UseShellExecute = false
};

References: ProcessStartInfo.WorkingDirectory Property

IronGeek
  • 4,764
  • 25
  • 27
  • This will fail for UNC paths, check [this](https://stackoverflow.com/questions/2586719/setting-processstartinfo-workingdirectory-to-an-unc-path) to find a working solution for UNC working directories. – Martin Braun Jan 13 '22 at 01:52
1
System.Environment.CurrentDirectory = @"..\..\..";
System.Diagnostics.Process.Start("MyCommand", "arg1, arg2, arg3");
Thejaka Maldeniya
  • 1,076
  • 1
  • 10
  • 20
  • 6
    *Never* set a global setting when a local one will work just fine. `System.Environment.CurrentDirectory` changes the working directory for your whole process, that usually isn't quite what you want. Instead, you should use the `ProcessStartInfo.WorkingDirectory` property (with `UseShellExecute` as appropriate). – Luaan Jan 13 '14 at 08:45