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.