asp.net C # call to run the CMD command implementation method

Source: Internet
Author: User

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");

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.