Batch command is the fastest and most efficient command to execute. Because the batch command, plainly speaking, is the MS-DOS environment command, there are a lot of batch commands, are pure DOS commands.
However, although the batch command is powerful, there are shortcomings. Batch commands can only perform basic functions and cannot complete complex network functions. Therefore, in many cases, program developers often use a variety of development languages as development tools, with batch processing commands, to achieve powerful execution of faster projects.
Below, this station to you to introduce is, how in CS structure of C # program , call MS-DOS window , run multiple batch command .
I. Introduction of namespaces
First, in the CS file header, refer to the following code:
Using System.Diagnostics;
Second, the function code
public void Mybatcommand ()//Name
{
Three strings below, representing three batch commands
String MyDosComLine1, MyDosComLine2, MyDosComLine3;
MyDosComLine1 = "cd\";//returns the root directory command
MyDosComLine2 = "CD MyFiles";//Enter the MyFiles directory
MyDosComLine3 = "Copy * * * e:\";//copy and paste all files from the current directory to E Drive
Process myprocess = new process ();
MyProcess.StartInfo.FileName = "cmd.exe";//Open DOS control platform
MyProcess.StartInfo.UseShellExecute = false;
MyProcess.StartInfo.CreateNoWindow = true;//Whether the DOS window is displayed, true is hidden;
MyProcess.StartInfo.RedirectStandardInput = true;
MyProcess.StartInfo.RedirectStandardOutput = true;
MyProcess.StartInfo.RedirectStandardError = true;
Myprocess.start ();
StreamWriter sIn = myprocess.standardinput;//Standard input stream
Sin.autoflush = true;
StreamReader sOut = myprocess.standardoutput;//Standard input stream
StreamReader SERR = myprocess.standarderror;//standard error stream
Sin.write (MyDosComLine1 System.Environment.NewLine);//First DOS command
Sin.write (MyDosComLine2 System.Environment.NewLine);//Second DOS command
Sin.write (MyDosComLine3 System.Environment.NewLine);//Third DOS command
Sin.write ("Exit" System.Environment.NewLine);//Fourth DOS command, Exit DOS window
string s = Sout.readtoend ();//Read output information after executing DOS command
String er = Serr.readtoend ();//Read error message after executing DOS command
if (myprocess.hasexited = = False)
{
Myprocess.kill ();
MessageBox.Show (ER);
}
Else
{
MessageBox.Show (s);
}
Sin.close ();
Sout.close ();
Serr.close ();
Myprocess.close ();
}
Part of the code explains:
To run multiple batch commands in C #, we can use the following format to add multiple commands.
Sin.write (DOS command code System.Environment.NewLine);
Where the DOS command code is what you want to do with the batch command, and System.Environment.NewLine indicates that the line wraps after the batch command.
For example:
public
void
ExecuteCmd(
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
;
//写入命令
p.StandardInput.WriteLine(cmd);
p.StandardInput.WriteLine(
"exit"
);
//等待结束
p.WaitForExit();
p.Close();
}
|
For example:
- Doscommand DOS command statement statement One && statement two statements one execution success before executing two
- Public string Execute (string doscommand)
- {
- return Execute (Doscommand, 10);
- }
- <summary>
- Executes a DOS command that returns the output of a DOS command
- </summary>
- <param name= "doscommand" >dos command </param>
- <param name= "milliseconds" > Wait Time for command execution (in milliseconds),
- If set to 0, infinite wait </param>
- <returns> returns the output of the DOS command </returns>
- Public static string Execute (string command, int seconds)
- {
- string output = ""; //output string
- if (command ! = null &&!command. Equals (""))
- {
- Process Process = new process (); Create a Process object
- ProcessStartInfo startinfo = new ProcessStartInfo ();
- Startinfo.filename = "cmd.exe"; Set the command to be executed
- Startinfo.arguments = "/C" + command; "/C" means to exit immediately after executing the command.
- Startinfo.useshellexecute = false; Booting without using the system shell
- Startinfo.redirectstandardinput = false; Do not redirect input
- Startinfo.redirectstandardoutput = true; //redirect Output
- Startinfo.createnowindow = true; Do not create a window
- Process. StartInfo = StartInfo;
- Try
- {
- if (process. Start ())//Begin process
- {
- if (seconds = = 0)
- {
- Process. WaitForExit (); //Here Infinite wait process end
- }
- Else
- {
- Process. WaitForExit (seconds); //wait for the process to end, waiting time for the specified millisecond
- }
- Output = process. Standardoutput.readtoend (); //Read the output of the process
- }
- }
- Catch
- {
- }
- finally
- {
- if (process! = null)
- Process. Close ();
- }
- }
- return output;
- }
Using System;
Using System.Collections;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Xml.Linq;
Using System.Diagnostics;
Namespace WebForm
{
public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
Response.Write (Execommand ("ping www.126.com"));
}
public string Execommand (string commandtext)
{
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;
string stroutput = null;
Try
{
P.start ();
P.standardinput.writeline (CommandText);
P.standardinput.writeline ("Exit");
Stroutput = P.standardoutput.readtoend ();
p.WaitForExit ();
P.close ();
}
catch (Exception e)
{
Stroutput = E.message;
}
return stroutput;
}
}
}
Asp. NET Execute cmd command