Baraa
Baraa

Reputation: 697

Command line c# not working as intended

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.

Upvotes: 0

Views: 1620

Answers (1)

Peter Perot
Peter Perot

Reputation: 1003

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.

Upvotes: 1

Related Questions