How to implicitly run the CMD command line window in C #

Source: Internet
Author: User

The CMD command line of MS is an important operation interface. Some functions that are not easy to complete in C # can be easily implemented by using a few simple commands in CMD, if you can complete the CMD Window Function in C #, it will make our program much easier.
The following describes how to use the cmd.exe program in the C program without displaying the command line window interface.
As follows:

Copy codeThe Code is as follows: System. Diagnosties. Process p = new System. Diagnosties. Process ();
P. StartInfo. FileName = "cmd.exe"; // name of the program to be executed
P. StartInfo. UseShellExecute = false;
P. StartInfo. RedirectStanderInput = true; // the input information from the caller may be accepted.
P. StartInfo. RedirectStanderOutput = true; // the output information is obtained by the calling program.
P. StartInfo. CreateNoWindow = true; // The program window is not displayed.
P. Start (); // Start the program
// Send the input information to the CMD window:
P. StanderInput. WriteLine ("shutdown-r t 10"); // restart after 10 seconds (C # may be hard to do)
// Obtain the output information of 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, it is very convenient to use third-party programs to expand the C # function.

The detailed source code is as follows:Copy codeThe Code is as follows: using System;
Using System. Diagnostics;
Namespace Business
{
/// <Summary>
/// Command summary.
/// </Summary>
Public class Command
{
Private Process proc = null;
/// <Summary>
/// Constructor
/// </Summary>
Public Command ()
{
Proc = new Process ();
}
/// <Summary>
/// Execute the CMD statement
/// </Summary>
/// <Param name = "cmd"> the CMD command to be executed </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 alias name (.exe file) </param>
/// <Param name = "cmd"> command to be executed </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 the software
/// </Summary>
/// <Param name = "programName"> Software alias name (.exe file) </param>
Public void RunProgram (string programName)
{
This. RunProgram (programName ,"");
}
}
}

Call timeCopy codeThe Code is as follows: Command cmd = new Command ();
Cmd. RunCmd ("dir ");

Note the following when getting output information:

ReadtoEnd () is easy to get stuck:

Copy codeThe Code is as follows: [csharp] view plaincopyprint? String outStr = proc. StandardOutput. ReadtoEnd ();

String outStr = proc. StandardOutput. ReadtoEnd ();

More inclined to use ReadLine ():

Copy codeThe Code is 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.