Private Process CmdProcess;
Private List
//private string TempShow="";
//on or off the switch command of the subscript
Int index;
//initialization Process
Private void InitProcess ()
{
CmdProcess=new Process ();
//set to start the program
CmdProcess. StartInfo. FileName="CMD. Exe";
//set the parameter
//CmdProcess. StartInfo. The Arguments="ipconfig";
//set the start to the current project of sub line layer
CmdProcess. StartInfo. UseShellExecute=false;//whether or not to use the operating system shell startup
CmdProcess. StartInfo. RedirectStandardInput=true;//to accept input from the caller information
CmdProcess. StartInfo. RedirectStandardOutput=true;//by calling program output information
CmdProcess. StartInfo. RedirectStandardError=true;//to redirect the standard error output
CmdProcess. StartInfo. CreateNoWindow=true;//do not display program window
//to get the asynchronous subscription event
CmdProcess. OutputDataReceived +=new DataReceivedEventHandler (CmdMessage);
CmdProcess. ErrorDataReceived +=new DataReceivedEventHandler (CmdMessage);
//start
CmdProcess. Start ();
//asynchronous access to command line content
CmdProcess. BeginOutputReadLine ();
CmdProcess. BeginErrorReadLine ();
//CmdProcess.
//CmdProcess.
CmdProcess. StandardInput. AutoFlush=true;
}
Public void ShowMessage (string MSG)
{
TextBoxWatch. Text +="first" + I + "time, the value is" + MSG + "\ r \ n";
}
Private void CmdMessage (object sender, DataReceivedEventArgs e)
{
String data=https://bbs.csdn.net/topics/e.Data;
i++;
Enclosing the Invoke (new ShowTemp (ShowMessage), data);
//MessageBox. Show (" first "+ I +" time, the value is "+ data).
//the Console. WriteLine (data);
//this. TextBoxCMDShow. Text +=data;
//directly set is not a correct thread will need to use delegate class
SetTextBox (data);
}
Private int I=0;
//set to the message to TextBoxCMDShow
//as is an asynchronous Process need to use this method is called to set news delegate class
Private delegate void ShowTemp (string MSG);
Public void SetTextBox (string MSG)
{
//MessageBox. Show (" first "+ I +" time, the value is "+ MSG);
//if it is not the current thread using the delegate class
If (this. TextBoxCMDShow. InvokeRequired)
{
//create and execute delegate class
This Invoke (new setMessage (SetTextBox), MSG);
//MessageBox. Show (" create the delegate class "+ (i++));
}
The else
{
//MessageBox. Show (" won the Msg "+ Msg + j++);
This. TextBoxCMDShow. Text +=MSG + "\ r \ n";
//this. TextBoxCMDShow. ScrollToCaret ();
}
}
//CMD command
Private void SendCommand (string command)
{
//if in startCommand already executed command is not in the CMD
if (! StartCommand (command))
{
CmdProcess. StandardInput. WriteLine (command);
}
}
//no need special command CMD perform
Private bool StartCommand (string command)
{
String CMD=command. ToLower ();
If (CMD) Equals (" the clear ") | | CMD) Equals (" CLS "))
{
//CLS command
TextBoxCMDShow. Text="";
return true;
}
return false;
}
Public delegate void setMessage (string MSG);
//button click event execute the command
Private void ButtonCMDInput_Click (object sender, EventArgs e)
{
//need to perform the CMD command
String command=this. TextBoxCMDInput. Text. The Trim ();
If (command. The Length & gt; 0)
{
//execute the command
SendCommand (command);
//the current command is added to the history records
CommandList. Add (command);
index++;
//remove command
Enclosing TextBoxCMDInput. Text="";
}
//can be input in a row, a little bit of output
//SendCommand (" ping mail.longcheer.com ");
//SendCommand (" ipconfig ");
}
//keyboard events
Private void TextBoxCmdInput_KeyUp (object sender, KeyEventArgs e)
{
//enter event
If (e.K eyValue=13 https://bbs.csdn.net/topics/=
{
//execute the command
ButtonCMDInput_Click (null, null);
I=0;
}
//history context switching command
Else if (e.K eyValue=https://bbs.csdn.net/topics/=38)
{
//38
//ArgumentOutOfRangeException
If (commandList. Count & lt;=0 | | index & lt;=0) return;
The index -;
This. TextBoxCMDInput. Text=commandList [index];
}
Else if (e.K eyValue=40) https://bbs.csdn.net/topics/=
{
//under 40
//prevent subscript crossing the line
If (commandList. Count & lt;=0 | | index & gt;=commandList. Count) return;
index++;
This. TextBoxCMDInput. Text=commandList [index];
}
}
# endregion
Private void TextBoxCmdInput_TextChanged (object sender, EventArgs e)
{
}
Private void TextBoxCMDShow_TextChanged (object sender, EventArgs e)
{
This. TextBoxCMDShow. SelectionStart=this. TextBoxCMDShow. Text. Length;
//select start position, in the end of the string length is the length of the
Enclosing TextBoxCMDShow. SelectionLength=0;
//set the selected position of the length of a string of 0
This. TextBoxCMDShow. ScrollToCaret ();
}