Use the cmd.exe program and external program

Source: Internet
Author: User

Using system;
Using system. diagnostics;

Namespace applycmd
{
///

/// Summary of cmdutility.
///

Public class cmdutility
{

///

/// Execute the cmd.exe command
///

///Command text
/// Command output text
Public static string execommand (string commandtext)
{
Return execommand (New String [] {commandtext });
}
///

/// Execute multiple cmd.exe commands
///

///Command text Array
/// Command output text
Public static string execommand (string [] commandtexts)
{
PROCESS p = new process ();
P. startinfo. filename = "cmd.exe ";
P. startinfo. useshellexecute = false;
P. startinfo. redirectstandardinput = true;
P. startinfo. redirectstandardoutput = true;
P. startinfo. redirectstandarderror = true;
P. startinfo. createnowindow = true;
String stroutput = NULL;
Try
{
P. Start ();
Foreach (string item in commandtexts)
{
P. standardinput. writeline (item );
}
P. standardinput. writeline ("exit ");
Stroutput = P. standardoutput. readtoend ();
P. waitforexit ();
P. Close ();
}
Catch (exception E)
{
Stroutput = E. message;
}
Return stroutput;
}
///

/// Start an external windows application to hide the Program Interface
///

///Application Path Name
/// True indicates success, and false indicates failure.
Public static bool Startapp (string appname)
{
Return Startapp (appname, processwindowstyle. Hidden );
}
///

/// Start an External Application
///

///Application Path Name
///Process Window Mode
/// True indicates success, and false indicates failure.
Public static bool Startapp (string appname, processwindowstyle style)
{
Return Startapp (appname, null, style );
}
///

/// Start an external application to hide the Program Interface
///

///Application Path Name
///Startup parameters
/// True indicates success, and false indicates failure.
Public static bool Startapp (string appname, string arguments)
{
Return Startapp (appname, arguments, processwindowstyle. Hidden );
}
///

/// Start an External Application
///

///Application Path Name
///Startup parameters
///Process Window Mode
/// True indicates success, and false indicates failure.
Public static bool Startapp (string appname, string arguments, processwindowstyle style)
{
Bool blnrst = false;
PROCESS p = new process ();
P. startinfo. filename = appname; // EXE, bat and so on
P. startinfo. windowstyle = style;
P. startinfo. Arguments = arguments;
Try
{
P. Start ();
P. waitforexit ();
P. Close ();
Blnrst = true;
}
Catch
{
}
Return blnrst;
}
}
}

PS: Use System. Diagnostics. Process to compress files or folders

String strarg = "a-r {0} {1 }";
System. diagnostics. process. start (@ "C:/program files/WinRAR/rar.exe", String. format (strarg, txtapp. text + ". RAR ", txtapp. text ));

Strarg is the command parameter of WinRAR. See help.

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.