12

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"); 
tshepang
  • 12,111
  • 21
  • 91
  • 136
user393148
  • 897
  • 4
  • 16
  • 27
  • I think you have to leave psi.FileName and psi.Arguments as in my code, and just do a p.StandardInput.WriteLine(@"c:\test.cmd arg0 arg1 arg2"); – Andrea Parodi Sep 01 '10 at 18:00
  • Thanks I tried that and it worked. However, with this I am trying to read the console log which does not return the output I need. Here is my complete code for clarification. What I am expecting is the output of command123. – user393148 Sep 01 '10 at 19:58
  • Process p = new Process(); p.StartInfo = psi; p.Start(); p.StandardInput.WriteLine(@"c:\test.cmd arg1 arg2 && cd /d c:\testdir\sample && command123 /c"); p.StandardInput.WriteLine(@"exit"); string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); – user393148 Sep 01 '10 at 19:59
  • The output I get is the above commands being run in console. How can I get the output of command123? – user393148 Sep 01 '10 at 19:59

4 Answers4

13

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...

Andrea Parodi
  • 5,534
  • 27
  • 46
2

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#.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
0

What are you trying to achieve? Do you actually need to open a command window, or do you need to simply make a directory, for example?

mkdir is a windows executable - you can start this program in the same way you start cmd - there's no need to start a command window process first.

You could also create a batch file containing all the commands you want to run, then simply start it using the Process and ProcessStartInfo classes you're already using.

Winston Smith
  • 21,585
  • 10
  • 60
  • 75
  • Winston, mkdir was only an example. I am launching a new cmd environment (basically that store a preset of env variables) and i want to run further commands. Andrea's answer should solve my problem. But doesnt seem to be working for me. – user393148 Sep 01 '10 at 17:24
0

How come this doesn't work?

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

    var process = new Process { StartInfo = startInfo };

    process.Start();
    process.StandardInput.WriteLine(@" dir");
    process.WaitForExit();
El Duderino
  • 55
  • 1
  • 6