2014-04-03 179 views
0

我編寫測試控制檯程序。該程序使用兩行命令執行cmd。但它是如何做的? 而不是這個大代碼,如何編寫更簡單的代碼?在cmd上執行兩個命令

String command = @"cd c:\\test";//command get to current folder 
ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.CreateNoWindow = true; 
startInfo.UseShellExecute = false; 
startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
startInfo.FileName = "cmd.exe"; 
startInfo.Arguments = command; 
startInfo.RedirectStandardOutput = true; 
using (Process exeProcess = Process.Start(startInfo)) 
{ 
    exeProcess.WaitForExit(); 
} 
String command = @"echo 'Hello world' > test.txt";//command write Hello world to text file 
ProcessStartInfo startInfo = new ProcessStartInfo(); 
startInfo.CreateNoWindow = true; 
startInfo.UseShellExecute = false; 
startInfo.WindowStyle = ProcessWindowStyle.Hidden; 
startInfo.FileName = "cmd.exe"; 
startInfo.Arguments = command; 
startInfo.RedirectStandardOutput = true; 
using (Process exeProcess = Process.Start(startInfo)) 
{ 
    exeProcess.WaitForExit(); 
} 

回答

0

你可以把你在批處理文件yourcmd.bat命令,你的情況yourcmd.bat會喜歡這個:

cd c:\test 
echo "Hello world" > test.txt 

cd c:\test & echo "Hello world" > test.txt 

那麼你可以調用System.Diagnostics.Process.Start("yourcmd.bat");方法,這個工程。