C # Run the doc command

Source: Internet
Author: User
Public   Class Testcmd
{
Public Void Operationcmd ()
{
String Dosstring1 = " Command 1 " ;
String Dosstring2 = " Command 2 " ;
Execute (dosstring1 );
Execute (dosstring2 );

}

Public String Execute ( String Doscommand)
{
Return Execute (doscommand, 6 * 1000 );
}

/// <Summary>
/// Run the doscommand to return the doscommand output.
/// </Summary>
/// <Param name = "doscommand"> Doscommand </Param>
/// <Param name = "milliseconds"> The time (in milliseconds) for waiting for command execution. If the value is set to 0, the system waits infinitely. </Param>
/// <Returns> Returns the doscommand output. </Returns>
Public Static String Execute ( String Doscommand, Int Milliseconds)
{
String Output ="" ; // Output string
If (Doscommand! = Null & Doscommand! = "" )
{
Process = New Process (); // Create process object
Processstartinfo startinfo = New Processstartinfo ();
Startinfo. filename = " Cmd.exe " ; // Set the command to be executed
Startinfo. Arguments = " /C " + Doscommand; // Set parameters. "/C" indicates that the system exits immediately after the command is executed.
Startinfo. useshellexecute = False ;// No System ShellProgramStart
Startinfo. redirectstandardinput = False ; // Do not redirect Input
Startinfo. redirectstandardoutput = True ; // Redirection output
Startinfo. createnowindow = True ; // Do not create a window
Process. startinfo = startinfo;
Try
{
If (Process. Start ()) // Start Process
{
If (Milliseconds = 0 )
Process. waitforexit (); // Wait for the process to end infinitely.
Else
Process. waitforexit (milliseconds ); // Wait for the process to end. The wait time is specified in milliseconds.
Output = process. standardoutput. readtoend (); // Read process output
}
}
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.