0

Previously somebody has asked how to run a command line command in C# from visual studio and the beneath was the answer.
I tried the same intended to call a tool called cccc which can run on command line. But when I run the beneath code I do not get any results and nothing shows wrong.
Stating generally how can we run the same commands from C# as it was in command line and get the same results. Say I call a program (it could be any program that is able to run on command line, for instance, cccc, ccm, and so on) on command line and get some results. How to call the command line and give the arguments so it will call in its turn the cccc or whatever and do the same thing as it was without C#.

string strCmdText;
strCmdText = "/C d: cd D:\\Exercises\\npp52\\PowerEditor\\src && dir /s /b | cccc - --outdir=d:\\myfolder";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • Please format your code in the question to make it readable ..also have you debugged this ..? if so please post the line(s) of code where you suspect the discrepancy is happening – MethodMan Dec 11 '12 at 17:00

4 Answers4

1

Add 'pause' to the end of your command:

string strCmdText;
strCmdText = "/C d: cd D:\\Exercises\\npp52\\PowerEditor\\src && dir /s /b | cccc - --outdir=d:\\myfolder & pause";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);

or redirect console standard output to a stream.

Nemanja Boric
  • 21,627
  • 6
  • 67
  • 91
1

Here need more magic with OutputDataReceived handler

void Main()
{
    System.Diagnostics.Process proc = new System.Diagnostics.Process();                     
                        proc.StartInfo.FileName="cmd.exe";
                        proc.StartInfo.Arguments = "/c ping 127.0.0.1";
                        proc.StartInfo.UseShellExecute = false;
                        proc.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
                        proc.StartInfo.RedirectStandardOutput = true;   
                        proc.Start();                               
                        proc.BeginOutputReadLine();                     
                        proc.WaitForExit();                             
                        proc.Close();
}

private void SortOutputHandler(object sendingProcess, 
            DataReceivedEventArgs outLine)
        {
            if (!String.IsNullOrEmpty(outLine.Data))
            {
             // Do what You need with out
             Console.WriteLine(outLine.Data);
            }
        }
Roman Melnyk
  • 1,097
  • 1
  • 8
  • 21
0

Instead of trying to put everything inside a string you could take advantage of the ProcessStartInfo class to better define your arguments

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.WorkingDirectory = "D:\\Exercises\\npp52\\PowerEditor\\src ";
psi.Arguments = "/C dir /s /b | cccc - --outdir=d:\\myfolder"";
psi.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(psi);

Also with the command window open you could see if there are syntax errors in your command

Steve
  • 213,761
  • 22
  • 232
  • 286
0

Another problem is that you are not using RedirectStandardOutput, so output is discarded. Take a look at this answer.

Community
  • 1
  • 1
Dour High Arch
  • 21,513
  • 29
  • 75
  • 90