How to implicitly run the cmd command-line window in C # _c# tutorial

Source: Internet
Author: User
Tags readline

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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.