Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start command windows and run commands inside

I need to start the command window with some arguments and run more commands inside.

For example, launch a test.cmd and run mkdir.

I can launch the test.cmd with processstartinfo , but i am not sure how to run further commands. Can I pass further arguments to the test.cmd process?

How do I go about this?

Unable to add comments to answer... SO writing here.

Andrea, This is what I was looking for. However the above code doesnt work for me.

I am launching a test.cmd which is new command environment (like razzle build environment) and I need to run further commands.

psi.FileName = @"c:\test.cmd";
psi.Arguments = @"arg0 arg1 arg2";

psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.UseShellExecute = false;

Process p = new Process();
p.StartInfo = psi;
p.Start();
p.StandardInput.WriteLine(@"dir>c:\results.txt");
p.StandardInput.WriteLine(@"dir>c:\results2.txt"); 
like image 929
user393148 Avatar asked Sep 01 '10 07:09

user393148


People also ask

How do you launch a Command Prompt from the Run line within Windows?

Open Command Prompt from the Run BoxPress Windows+R to open “Run” box. Type “cmd” and then click “OK” to open a regular Command Prompt. Type “cmd” and then press Ctrl+Shift+Enter to open an administrator Command Prompt.

How do you Run an internal command?

Internal commands can be run from the command prompt only, which means that you have to open the command shell using cmd /c or cmd /k before specifying the command.

How do I Run a process in the background in Windows?

Select Start , then select Settings > Privacy > Background apps. Under Background Apps, make sure Let apps run in the background is turned On. Under Choose which apps can run in the background, turn individual apps and services settings On or Off.


2 Answers

The /c parameter to cmd.

ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/c pause");
Process.Start(start);

(pause is just an example of what you can run)

But for creating a directory you can do that and most other file operations from c# directly

System.IO.Directory.CreateDirectory(@"c:\foo\bar");

Start a cmd from c# is useful only if you have some big bat-file that you don't want to replicate in c#.

like image 131
Albin Sunnanbo Avatar answered Sep 30 '22 02:09

Albin Sunnanbo


You can send further commands to cmd.exe using the process standard input. You have to redirect it, in this way:

var startInfo = new ProcessStartInfo
                    {
                        FileName = "cmd.exe",
                        RedirectStandardInput = true,
                        RedirectStandardOutput = true,
                        UseShellExecute = false,
                        CreateNoWindow = true
                    };

var process = new Process {StartInfo = startInfo};

process.Start();
process.StandardInput.WriteLine(@"dir>c:\results.txt");
process.StandardInput.WriteLine(@"dir>c:\results2.txt");
process.StandardInput.WriteLine("exit");

process.WaitForExit();

Remember to write "exit" as your last command, otherwise the cmd process doesn't terminate correctly...

like image 40
Andrea Parodi Avatar answered Sep 30 '22 02:09

Andrea Parodi