简体   繁体   中英

C# Run command on command prompt and show the command

I am trying to create an app that starts cmd.exe and send command. It is important that the command is visible on cmd. Here is that I got so far but it doesn't seem to be working. Any idea?

            Process myProc = new Process();
            myProc.StartInfo.FileName = "cmd.exe";
            myProc.StartInfo.RedirectStandardInput = true;
            myProc.StartInfo.RedirectStandardOutput = true;
            myProc.StartInfo.UseShellExecute = false;
            myProc.Start();
            StreamWriter sendCommand = myProc.StandardInput;
            sendCommand.WriteLine("run.exe --forever"); //I want this command to show up in cmd

When the code above is executed, run.exe is ran but the command does not show up in cmd. What am I doing wrong?

Here's an addendum to my comment to make it clearer:

Process myProc = new Process();
myProc.StartInfo.FileName = "cmd.exe";
myProc.StartInfo.RedirectStandardInput = true;
//myProc.StartInfo.RedirectStandardOutput = true;
myProc.StartInfo.UseShellExecute = false;
myProc.Start();
System.IO.StreamWriter sendCommand = myProc.StandardInput;
sendCommand.WriteLine("run.exe --forever");

This will allow everything outputted by cmd to show in the cmd console.

why you are using the streamwriter ? you can use the Arguments

 myProc.StartInfo.Arguments="run.exe --forever";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM