0

I have the following command that I wish to run (which works when I manually run it in cmd):

"C:\Program Files\APP\APP.exe" -CMV "C:\Program Files\APP\Second\IT" -ID 5 6 7 4 2

This is the code that I have written in C#:

string firstPath = @"""C:\Program Files\APP\APP.exe""";
string secondPath = @"""C:\Program Files\APP\Second\IT""";
string command = firstPath + " -CMV " + secondPath + " -ID 5 6 7 4 2");

I also tried the following piece of code:

int exitCode;
ProcessStartInfo processInfo;
Process process;

processInfo = new ProcessStartInfo("cmd.exe", command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
// *** Redirect the output ***
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;

//processInfo.Arguments 
process = Process.Start(processInfo);
process.WaitForExit();

// *** Read the streams ***
// Warning: This approach can lead to deadlocks, see Edit #2
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();

exitCode = process.ExitCode;

Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
process.Close();

This still doesn't seem to work! Any help is much appreciated.

*********FINAL EDIT*********

This is the string that gets sent in as the command:

""C:\\Program Files\\DEEM\\DEEM.exe" -ENV "C:\\Program Files\\DEEM\\Environments\\IT" -ID 01004698001001 00285209090217 00285209090250 00285209090382 99041250643762"

Which results in the following error:

"'C:\Program' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"

All I need to make it work is to take out the first and last quote which are wrapped due to the command being a string. I am not sure how to take the quotes out.

M. Schena
  • 2,039
  • 1
  • 21
  • 29
Baraa
  • 687
  • 1
  • 9
  • 26
  • 2
    When you run i manually you write -ENV, when runned programatically you write -CMV. Could that be something? – iikkoo May 31 '16 at 12:45
  • 1
    @iikkoo your attention is impressive but that was a mistake on my part writing this up the are currently ran as the same variable! Sorry I edited the mistake – Baraa May 31 '16 at 12:48
  • IIRC, you need "cmd.exe /c ..." – 001 May 31 '16 at 12:48
  • @JohnnyMopp I just tried changing the startInfo.FileName to "cmd.exe /c" it gave the following exception: 'System.ComponentModel.Win32Exception' occurred in System.dll but was not handled in user code' – Baraa May 31 '16 at 12:52
  • @JohnnyMopp I changed the following line above (in the second method) processInfo = new ProcessStartInfo("cmd.exe", command); to processInfo = new ProcessStartInfo("cmd.exe", "/c " + command); and now I get the following error "'C:\\Program' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" – Baraa May 31 '16 at 13:02
  • 1
    Try putting he whole thing after `/c` in quotes and add `/s`. Read [this](http://stackoverflow.com/a/356014/669576) – 001 May 31 '16 at 13:14
  • @JohnnyMopp I found out that this is the output that it is reading it as exactly: ""C:\\Program Files\\DEEM\\DEEM.exe" -ENV "C:\\Program Files\\DEEM\\Environments\\IT" -ID 01004698001001 00285209090217 00285209090250 00285209090382 99041250643762" So the only difference is I don't need the first and last quote for it to work that is my error – Baraa May 31 '16 at 13:16
  • 1
    That's what the `/s` parameter is for. It strips the first and last quotes. So you should have `new ProcessStartInfo("cmd.exe", "/c /s " + command);`. Where `command` is the command line in quotes: `""C:\Program Files\DEEM\DEEM.exe" -ENV "C:\Program Files\DEEM\Environments\IT" -ID 01004698001001 00285209090217 00285209090250 00285209090382 99041250643762"` – 001 May 31 '16 at 13:35
  • @JohnnyMopp I was just going to respond to you and let you know that it worked please submit an answer so I can accept! Thanks! – Baraa May 31 '16 at 13:39

1 Answers1

1

Your first example works if you wait for the process to exit:

    static void Main(string[] args)
    {
        string firstPath = @"""C:\Program Files\APP\APP.exe""";
        string secondPath = @"""C:\Program Files\APP\Second\IT""";
        string command = firstPath + " -CMV " + secondPath + " -ID 5 6 7 4 2";

        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        //startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = firstPath; //"cmd.exe";
        startInfo.Arguments = command;
        process.StartInfo = startInfo;
        process.Start();

        // **** don't forget to wait for the process to exit ***
        process.WaitForExit();

        var code = process.ExitCode;
        var time = process.ExitTime;
    }

In your second example you did use WaitForExit(), but there is some other stuff broken I did not investigate on.

Peter Perot
  • 1,003
  • 1
  • 10
  • 15
  • Hmm I tried to wait but it still didn't work :(...I edited the main question with a final edit in which I believe is the issue that is causing the command not to run. I need to get rid of the first and last quote created due to the string being a string... – Baraa May 31 '16 at 13:23