How to execute the doscommand in the ASP. NET Program

Source: Internet
Author: User

/// <Summary>
/// Execute the doscommand and return the doscommand output
/// </Summary>
/// <Param name = "doscommand"> doscommand </param>
/// <Param name = "milliseconds"> the waiting time (unit: milliseconds) for command execution. If it is set to 0, the waiting time is unlimited. </param>
/// <Returns> returns the output. If an exception occurs, an empty string is returned. </returns>
Public static string executedos (string doscommand, int milliseconds)
{
String output = ""; // output string
If (doscommand! = NULL & doscommand! = "")
{
Process = new process (); // create a process object
Processstartinfo startinfo = new processstartinfo ();
Startinfo. filename = "cmd.exe"; // set the command to be executed
Startinfo. Arguments = "/C" + doscommand; // set the parameter. "/C" indicates that the system exits immediately after the command is executed.
Startinfo. useshellexecute = false; // The system shell is not used. Program Start
Startinfo. redirectstandardinput = false; // The input is not redirected.
Startinfo. redirectstandardoutput = true; // redirect output
Startinfo. createnowindow = true; // do not create a window
Process. startinfo = startinfo;
Try
{
If (process. Start () // start the process
{
If (milliseconds = 0)
Process. waitforexit (); // There is no limit to wait until the process ends.
Else
Process. waitforexit (milliseconds); // wait until the process ends. The wait time is specified in milliseconds.
Output = process. standardoutput. readtoend (); // read the output of the process.
}
}
Catch
{
}
Finally
{
If (process! = NULL)
Process. Close ();
}
}
Return output;
}

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.