命令语法的ProcessStartInfo

问题描述:

是否有人可以帮助我走出这里的语法命令语法的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"); 

你需要调试,我会从这里开始:

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

没有什么真的看起来错了。也许调试它,以便您可以从中获取文本并将其复制/粘贴到资源管理器,以确保路径有效,并且没有什么奇怪的事情发生?

此外,您不需要对.Text属性的文本框做.ToString()。它已经是一个字符串了。