Call the cmd.exe program and external programs (transfer)

zhaozj2021-02-16  57

Using system; using system.diagnostics;

Namespace ApplyCmd {///

/// cmdutility's summary description. ///

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.uShellexecute = false;

P.Startinfo.redirectStandardInput = true;

P.Startinfo.RedirectStandardOrdoutput = 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 the external Windows application, hide the program interface ///

///

Application path name

///

True said successfully, FALSE said failed

Public Static Bool StartApp (String Appname)

{

Return StartApp (Appname, ProcessWindowStyle.hidden);

}

///

/// Start the external application ///

///

Application path name

///

Process window mode

///

True said successfully, FALSE said failed

Public Static Bool StartApp (String Appname, ProcessWindowStyle Style)

{

Return StartApp (AppName, Null, Style);

}

///

/// Start the external application, hide the program interface ///

///

Application path name

///

Start parameters

///

True said successfully, FALSE said failed

Public Static Bool StartApp (String Appname, String Arguments)

{

Return Startapp (AppName, Arguments, ProcessWindowStyle.hidden);

}

///

/// Start the external application ///

///

Application path name

///

Start parameters

///

Process window mode

///

True said successfully, FALSE said failed

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: Using System.Diagnostics.Process to compress file or folder

String string strunt = "a -r {0} {1}"; system.diagnostics.process.start (@ "c: / program files / winrar / rar.exe", string.format (strarg, txtapp.text ". RAR ", txtapp.text));

STRARG is a command parameter for WinRAR, please refer to the help.

转载请注明原文地址:https://www.9cbs.com/read-17349.html

New Post(0)