要執行如下的Windows command (Unpack tar and gz
files)
start "" "C:\Program
Files\7-Zip\7z.exe"
"C:\Program Files\7-Zip\7z" x
"D:\ Files\*.tar" -o" D:\ Files" > err.log
"C:\Program Files\7-Zip\7z" x
" D:\ Files\*.gz" -o" D:\ Files" > err.log
String cmd1 = String.Format("start
\"\" \"{0}\"",Path.Combine(ZIP_PATH, "7z.exe"));
String cmd2 = …
String cmd3
= …
1. 方法一 寫入bat檔案,再執行bat
String batFileName = "UnpackTar.bat";
StreamWriter sw;
sw = new StreamWriter(batFileName, false);
sw.WriteLine(cmd1);
sw.WriteLine(cmd2);
sw.WriteLine(cmd3);
sw.Close();
var p = Process.Start(batFileName);
p.WaitForExit(); //等待Process執行完畢
|
2. 方法二 直接起CMD.exe執行windows command line
String strCmdText = String.Format("{0}{1} &
{2} & {3}", "/C ", cmd1, cmd2, cmd3);
var p = Process.Start("CMD.exe", strCmdText);
p.WaitForExit(); //等待Process執行完畢
|
注意在command前面要加個”/C ”
3. 方法三 直接起CMD.exe執行windows command line (Hide CMD
window)
System.Diagnostics.Process process = new
System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new
System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle
= System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName
= "cmd.exe";
startInfo.Arguments
= String.Format("{0}{1} &
{2} & {3}", "/C ", cmd1, cmd2, cmd3);
process.StartInfo
= startInfo;
process.Start();
process.WaitForExit(); //等待Process執行完畢
|
ð 利用”&” 來將三個指令合併成one line command
Reference