Ms cmd command line is an important operating interface, some in C # is not so convenient to complete the function, in the cmd several simple commands may be easy to handle, if can be in C # to complete the functions of the CMD window, it must make our program simple and many.
ASP tutorial. NET C # source code
using System;
Using System.Diagnostics;
namespace Business
{
<summary>
A summary description of the command.
</summary>
public class command
{
Private process proc = null;
<summary>
Construction method
</summary>
Public command ()
{
proc = new process ();
}
<summary>
Execute CMD statement
</summary>
<param name= "cmd" > Cmd command to execute </param>
public void Runcmd (string cmd)
{
Proc.startinfo.createnowindow = true;
Proc.startinfo.filename = "cmd.exe";
Proc.startinfo.useshellexecute = false;
Proc.startinfo.redirectstandarderror = true;
Proc.startinfo.redirectstandardinput = true;
Proc.startinfo.redirectstandardoutput = true;
Proc.start ();
Proc.standardinput.writeline (CMD);
Proc.close ();
}
<summary>
Open the software and execute the command
</summary>
<param name= "ProgramName" > Software path plus name (. exe file) </param>
<param name= "cmd" > Command to execute </param>
public void Runprogram (string programname,string cmd)
{
Process proc = new process ();
Proc.startinfo.createnowindow = true;
Proc.startinfo.filename = ProgramName;
Proc.startinfo.useshellexecute = false;
Proc.startinfo.redirectstandarderror = true;
Proc.startinfo.redirectstandardinput = true;
Proc.startinfo.redirectstandardoutput = true;
Proc.start ();
if (cmd.length!= 0)
{
Proc.standardinput.writeline (CMD);
}
Proc.close ();
}
<summary>
Open Software
</summary>
<param name= "ProgramName" > Software path plus name (. exe file) </param>
public void Runprogram (string programname)
{
This.runprogram (ProgramName, "");
}
}
}
BAT processing File
System.diagnosties.process p=new system.diagnosties.process ();
P.startinfo.filename= "cmd.exe";//The name of the program to execute
P.startinfo.useshellexecute=false;
p.startinfo.redirectstanderinput=true;//may accept input from the calling program
p.startinfo.redirectstanderoutput=true;//the caller to get output information
p.startinfo.createnowindow=true;//does not show program windows
P.start ()//Start Program
To send input information to the CMD window:
P.standerinput.writeline ("Shutdown-r t 10"); Reboot in 10 seconds (C # can not do well)
Get the output information for the cmd window:
String soutput = P.standardoutput.readtoend (); With the following code, you can operate CMD without knowing it. In short, the process class is a very useful class that facilitates the extension of C # 's functionality using Third-party programs.
Call method
command cmd = new command ();
Cmd.runcmd ("dir");