Private Void Button#click ( Object Sender, eventargs E)
{
// Call Code
String [] Cmd = New String [] { "Ping 192.168.3.15-N 1" , "Ping 192.168.3.16-N 2" };
MessageBox. Show (CMD ));
Closeprocess ( "Cmd.exe" );
}
/// <Summary>
/// Run the CMD command
/// </Summary>
/// <Param name = "Cmd" > Command </param>
/// <Returns> </returns>
Public Static String CMD ( String [] Cmd)
{
PROCESS p = New 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 ();
P. standardinput. autoflush = True ;
For ( Int I = 0; I <cmd. length; I ++)
{
P. standardinput. writeline (CMD [I]. tostring ());
}
P. standardinput. writeline ( "Exit" );
String Strrst = P. standardoutput. readtoend ();
P. waitforexit ();
P. Close ();
Return Strrst;
}
/// <Summary>
/// Close the process
/// </Summary>
/// <Param name = "Procname" > Process name </param>
/// <Returns> </returns>
Public Static Bool Closeprocess ( String Procname)
{
Bool Result = False ;
System. Collections. arraylist proclist = New System. Collections. arraylist ();
String Tempname = "" ;
Int Begpos;
Int Endpos;
Foreach (System. Diagnostics. Process thisproc In System. Diagnostics. process. getprocesses ())
{
Tempname = thisproc. tostring ();
Begpos = tempname. indexof ( "(" ) + 1;
Endpos = tempname. indexof ( ")" );
Tempname = tempname. substring (begpos, endpos-begpos );
Proclist. Add (tempname );
If (Tempname = procname)
{
If (! Thisproc. closemainwindow ())
Thisproc. Kill (); // forcibly terminate the process when the command for sending and closing the window is invalid.
Result = True ;
}
}
Return Result;
}