I have been toying with the Process class in C#. I have some code below I'd like to use to open cmd.exe and run the DIR command. However, when I attempt to use the code, the cmd.exe opens, but no command is run. Why is this happening and how do I fix it?
Process cmd = new Process();
cmd.StartInfo.FileName = @"cmd.exe";
cmd.StartInfo.Arguments = @"DIR";
cmd.Start();
cmd.WaitForExit();
cmd.StartInfo.Arguments = @"/c DIR";
Try passing the
/K
option to let the command console stay at video and receive the subsequent DIR command (Without exit).The /K option will allow you to get a better understanding of what happens in the command window because the window will not close immediately and you need to click the close button or type the Exit command. If you want to exit after issuing your commands then use the
/C
option.