- I want to run a series of commands on cmd.exe from C#.
- I need to open only one window of cmd
- I need to keep the cmd window open through the execution and after the completion.
- I need to display the commands [edit]in the opened cmd window[/edit] executed as well as the output of the commands.
So basically I want to open and use the cmd.exe just like a manual user would. I tried some methods, but none could do all 4 items above.
Below code works but does not display the commands/outputs and terminates after completion. Any help?
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.CreateNoWindow = false;
info.Arguments = "/k";
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("dir");
sw.WriteLine("ipconfig");
}
}