2013-07-08 36 views
0

是否有人可以幫助我走出這裏的語法命令語法的ProcessStartInfo

string sourcePath = @textBox2.Text.ToString(); 
string targetPath = @textBox1.Text.ToString(); 

System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"XCOPY C:\folder D:\Backup\folder /i"); 

psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
psi.UseShellExecute = false; 
System.Diagnostics.Process copyFolders = System.Diagnostics.Process.Start(psi); 
copyFolders.WaitForExit(); 

我想下面更改此行來替換C:\文件夾(SOURCEPATH)和D:\Backup\folder與TARGETPATH

(@"XCOPY C:\folder D:\Backup\folder /i"); 

我不斷收到沒有找到來源,當我的MessageBox這樣它看起來不錯

MessageBox.Show(@"XCOPY " + (sourcePath) + (targetPath) + " /i"); 

回答

0

你需要調試,我會從這裏開始:

string args = string.format("{0} {1} /i", sourcePath, targetPath); 
Debug.WriteLine(args); 
//verify your paths (both) are correct 

string cmd = string.format("XCOPY {0}", args); 
Debug.WriteLine(cmd); 
//copy the command and test it out directly in cmd 

另外,儘量使你的論點....作爲參數....

System.Diagnostics.ProcessStartInfo psi = 
    new System.Diagnostics.ProcessStartInfo("XCOPY", args); 

你可以看看在the documentation爲傳遞與自變量的示例

+0

感謝Mike C使用此代碼 string args =(sourcePath)+「」+(targetPath); System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(「XCOPY」,args +「/ i」); – Zen

0

沒有什麼真的看起來錯了。也許調試它,以便您可以從中獲取文本並將其複製/粘貼到資源管理器,以確保路徑有效,並且沒有什麼奇怪的事情發生?

此外,您不需要對.Text屬性的文本框做.ToString()。它已經是一個字符串了。