Winformsapp_operateandinputcmd:
Create a new Form1, drag into the TextBox, set to allow multiple rows, dock to fill, and then bind the KeyUp event
The execution code is as follows:
Copy Code code as follows:
private void Txtcmdinput_keyup (object sender, KeyEventArgs e)
{
if (E.keycode = = Keys.enter)
{
int count = TxtCmdInput.Lines.Length;
if (count = 0) return;
while (Count > 0 && (String. IsNullOrEmpty (Txtcmdinput.lines[count-1]))
{
count--;
}
if (Count > 0)//&&!string. IsNullOrEmpty (Txtcmdinput.lines[count-1])
Executecmd (Txtcmdinput.lines[count-1]);
}
}
public void Executecmd (string cmd)
{
System.Diagnostics.Process p = new System.Diagnostics.Process ();
p.StartInfo.FileName = "cmd.exe";
P.startinfo.useshellexecute = false;
P.startinfo.redirectstandardinput = true;
P.startinfo.redirectstandardoutput = true;
P.startinfo.redirectstandarderror = true;
P.startinfo.createnowindow = true;
P.start (); Set automatic refresh buffering and update
P.standardinput.autoflush = true; Write command
P.standardinput.writeline (CMD);
P.standardinput.writeline ("Exit"); Waiting for the end
Txtcmdinput.appendtext (P.standardoutput.readtoend ());
p.WaitForExit ();
P.close ();
}
Execute the effect chart: