1

I want to run multiple CMD command from C# application.

The command on the cmd is like that "C:\Users\Sara Mamdouh\Desktop\New folder> hvite -T 01 -C hcon.con -w net dict hmm_list Ann.wav".

My question is how to call this command from the C# application and also receive the results in a string?

Soner Gönül
  • 91,172
  • 101
  • 184
  • 324

4 Answers4

2

First of all, you should write this in a bat file and save it as a like bat.bat.

cd \
cd C:\Users\Sara Mamdouh\Desktop\New folder
exit

You can use Process.Start() method after that.

Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component.

Process p = new Process();
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "path to bat.bat";
ps.RedirectStandardInput = true;
ps.RedirectStandardOutput = true;
ps.UseShellExecute = false;
p.StartInfo.Arguments = "hvite -T 01 -C hcon.con -w net dict hmm_list Ann.wav";
p.StartInfo = ps;
p.Start();
string output = p.StandardOutput.ReadToEnd();
Soner Gönül
  • 91,172
  • 101
  • 184
  • 324
  • 1
    I know the OP wrote this in his question, but the "C:\...>" part is what he sees in the console window and can not be part of the command. – Thorsten Dittmar Mar 21 '13 at 12:58
  • @ThorstenDittmar You are totally right! It can't be a part of the command. Update is coming.. – Soner Gönül Mar 21 '13 at 13:01
  • This is the output that appears on the screen "c:\users\sara mamdouh\documents\visual studio 2010\Projects\HTK\HTK\bin\Debug". This doesn't include the required results. Where HTK is the application name. – Sara Mamdouh Mar 21 '13 at 13:09
  • It doesn't work, there is exception "The Process object must have the UseShellExecute property set to false in order to redirect IO streams." – Sara Mamdouh Mar 21 '13 at 13:24
  • I need help! There isn't any output that appear on the cmd. – Sara Mamdouh Mar 21 '13 at 13:44
  • I printed out the output it changed the directory but didn't execute the "hvite" command. The output was "C:\Users\Sara Mamdouh\Desktop\New folder>exit". – Sara Mamdouh Mar 21 '13 at 13:53
  • Thx, but can u help me out on this please!! – Sara Mamdouh Mar 21 '13 at 14:29
0

Read the documentation for the Process class.

As the informative output C:\Users\Sara Mamdouh\Desktop\New folder> you see in the console window may not be part of the command you're issuing, you need to add the absolute path names to the command, so the files can be found:

var cmd = @"\"<path to hvite>\hvite\" -T 01 -C hcon.con -w net dict hmm_list \"C:\Users\Sara Mamdouh\Desktop\New folder\Ann.wav\"";
System.Diagnostics.Process.Start("CMD.exe", cmd);
Thorsten Dittmar
  • 52,871
  • 8
  • 78
  • 129
  • What's not clear? When you open up the Windows Console you see the "path part" from above. This, however, is not part of the commands you issue - it's only there that you know in which folder you are. When you issue a command from your own application, it's best to use absolute path names whenever possible. This is what I'm saying above. – Thorsten Dittmar Mar 21 '13 at 13:43
0

You can use the Process.Start method if you just need to start a process:

System.Diagnostics.Process.Start("cmd" "whatever parameters");

For reading the output, look at the examples on the MSDN page for Process.StandardOutput:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

Basically, instead of just calling Process.Start, you create a new Process object with the required parameters, call Start() on it and then read the output as in the example.

Botz3000
  • 37,236
  • 8
  • 100
  • 125
0
var cmd = @"hvite -T 01 -C hcon.con -w net dict hmm_list Ann.wav";
System.Diagnostics.Process.Start("CMD.exe", cmd);
Alexander Tsvetkov
  • 1,599
  • 13
  • 24