0

I have some script files that would usually be edited and run through a ui, but the offer to run on the command line when using the syntax: program.exe scriptfile.fmw --userparameter "some parameter".

If I simply write

arguments = "program.exe scriptfile.fmw --userparameter \"some parameter\"";
Process process = Process.Start("cmd.exe", $"/K {arguments};
process.WaitForExit();

the commandline starts, calls the correct exe and runs the script.

Now I want to retrieve the response of the script after it ran to catch possible error messages.

As far as I can see this requires to use ProcessStartInfo and that's basically where my problem seems to start.

ProcessStartInfo startInfo = new ProcessStartInfo()
{
    FileName = "cmd.exe",
    Arguments = $"/K {arguments}",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};

Process process = Process.Start(startInfo);
process.BeginErrorReadLine();
process.WaitForExit();

The code above opens a commandline window, but never uses the arguments given. It escapes me on how I should hand the argument line over to the cmd.exe - process.

I was playing around with RedirectStandardInput and its StreamWriter, but never managed to get anything written into the cmd-window.

The same goes for the RedirectStandardOutput, where I would like to gather the script response in the cmd-window as a string or string[], to parse for specific exit codes of the script.

TomGeo
  • 1,213
  • 2
  • 12
  • 24
  • 4
    95% of issues in C# related to `cmd.exe` are solved by not using `cmd.exe`. Why not launch your actual process, and use `RedirectStandardOutput` etc _with it_ rather than `cmd.exe`? – mjwills Aug 03 '21 at 10:37
  • something else... the command line version of the ui, normally used to execute the scripts when user input is required. – TomGeo Aug 03 '21 at 10:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235582/discussion-between-tomgeo-and-mjwills). – TomGeo Aug 03 '21 at 10:47
  • Does this answer your question? [Passing an argument to cmd.exe](https://stackoverflow.com/questions/5047171/passing-an-argument-to-cmd-exe) and [c# run cmd with argument and close it in finish](https://stackoverflow.com/questions/40264719/c-sharp-run-cmd-with-argument-and-close-it-in-finish) –  Aug 03 '21 at 11:04
  • 1
    @OlivierRogier Best not to encourage the use of `cmd.exe`. What the OP needs is a duplicate showing how to process standard output / error. – mjwills Aug 03 '21 at 11:06

1 Answers1

4

I think this is what you want, have a look below :

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo()
{
    FileName = "program.exe", //You must use your program directly without invoking through cmd.exe
    Arguments = "scriptfile.fmw --userparameter \"some parameter\"", //Add parameters and arguments here needed by your application
    UseShellExecute = false,
    EnableRaisingEvents = true, //You are missing this
    RedirectStandardOutput = true,
    RedirectStandardError = true
};

process.ErrorDataReceived += process_ErrorDataReceived; //You should listen to its output error data by subscribing to this event
process.BeginErrorReadLine();
process.Start(startInfo);
process.WaitForExit(); // You may now avoid this

Then at here do anything with your received error data!

private static void process_ErrorDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
    DoSomething(e.Data); // Handle your error data here
}

EDIT-(1) : Please try this solution and ping me if it works or if you need some extra help. Everyone in comments is suggesting that you must not use cmd.exe to invoke your program as it may causes debugging overhead, performance issue and you might not get error details as well.

Cheers!