Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute command line from a specific folder [duplicate]

Tags:

c#

cmd

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

like image 417
user3150947 Avatar asked Jan 13 '14 08:01

user3150947


People also ask

How do I duplicate files using the command line?

Use the Copy Command to Transfer Specific Files To copy files, use the copy command from the command line. copy c:\myfile. txt e: The command above will copy "myfile.

How do you copy a folder in terminal?

Copy a Directory and Its Contents ( cp -r ) Similarly, you can copy an entire directory to another directory using cp -r followed by the directory name that you want to copy and the name of the directory to where you want to copy the directory (e.g. cp -r directory-name-1 directory-name-2 ).

How do I run multiple cp commands in Linux?

To copy multiple files you can use wildcards (cp *. extension) having same pattern. Syntax: cp *.


2 Answers

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

like image 154
IronGeek Avatar answered Oct 05 '22 11:10

IronGeek


System.Environment.CurrentDirectory = @"..\..\..";
System.Diagnostics.Process.Start("MyCommand", "arg1, arg2, arg3");
like image 22
Thejaka Maldeniya Avatar answered Oct 05 '22 09:10

Thejaka Maldeniya