0

I'm just trying to know how to run commands on the command line in C#, I've browsed just about every forum on this topic and all have something like this:

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "echo hello";
process.Start();

Yet when I type it in, all that shows up is a command prompt with the project directory. The command is never executed.

jazb
  • 5,498
  • 6
  • 37
  • 44
TermSpar
  • 401
  • 3
  • 13
  • The same thing happens if I open a command prompt and type `cmd.exe echo hello`. I imagine you need `cmd.exe /c echo hello` or `cmd.exe /k echo hello`, depending on the result you want. – ProgrammingLlama Dec 04 '18 at 02:43
  • cmd.exe is not part of the argument, it tells the computer what to run the command on – TermSpar Dec 04 '18 at 02:44
  • 3
    OK, go to Start | Run and then type "cmd.exe echo hello" and click OK. Does that give you the result you want? No it does not. Why doesn't it? Because that's not how cmd.exe works. I understand that cmd.exe is not part of the argument, but launching cmd.exe with the argument you've specified does not do what you think it does. That is my point. – ProgrammingLlama Dec 04 '18 at 02:45
  • `FileName = "cmd.exe"; Arguments = "/k command";` or `Arguments = "start /WAIT command";`. It depends on how you're using it. Sample application here: [How do I get output from a command to appear in a control on a form in real-time](https://stackoverflow.com/questions/51680382/how-do-i-get-output-from-a-command-to-appear-in-a-control-on-a-form-in-real-time?answertab=active#tab-top) – Jimi Dec 04 '18 at 03:49

0 Answers0