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.
Here's a common way to do all of the functions in CMD by calling the CMD.exe program in a C # program and not displaying the command Line window interface.
As shown below:
Copy Code code as follows:
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.
Detailed source code is as follows:
Copy Code code as follows:
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, "");
}
}
}
When called
Copy Code code as follows:
command cmd = new command ();
Cmd. Runcmd ("dir");
Get output information should be noted:
ReadtoEnd () easily jammed:
Copy Code code as follows:
[CSharp] View plaincopyprint?string outstr = Proc. Standardoutput.readtoend ();
String outstr = Proc. Standardoutput.readtoend ();
More inclined to use ReadLine ():
Copy Code code as follows:
[CSharp] View plaincopyprint?string tmptstr = Proc. Standardoutput.readline ();
String outstr = "";
while (Tmptstr!= "")
{
Outstr + = outstr;
Tmptstr = Proc. Standardoutput.readline ();
}