C#运行带有空格参数的batch file

我需要运行batch file,其中有path(可以包含空格)作为参数。

batch file非常简单:

回声

echo%1> echotest.txt

我用来运行这个batch file的Csharp代码:

ProcessStartInfo info = new ProcessStartInfo(); info.UserName = KIM_USER; info.Password = ConvertToSecureString(KIM_USER_PASSWORD); info.FileName = theTask.Path; info.Arguments = "\"" + TranslateParameter(theTask.Parameter) + "\""; info.Domain = Environment.MachineName; info.WorkingDirectory = Path.GetDirectoryName(theTask.Path); info.UseShellExecute = false; info.CreateNoWindow = true; Process batProcess = Process.Start(info); batProcess.WaitForExit(); 

基本上参数来自例如{Test_Path},这是在TranslateParameter转换为真正的path,例如:D:\ Test Path \(包含空格)

这对我不起作用,它每次都返回我的退出代码1。

如果我从info.Arguments中删除\“,它的工作原理,但在输出文件只是D:\ Test

有什么build议么?

问候

Solutions Collecting From Web of "C#运行带有空格参数的batch file"

你可以试试这个,如果这样的作品:

 ProcessStartInfo info = new ProcessStartInfo(); info.UserName = KIM_USER; info.Password = ConvertToSecureString(KIM_USER_PASSWORD); info.FileName = theTask.Path + " \"" + TranslateParameter(theTask.Parameter) + "\""; //info.Arguments = "\"" + TranslateParameter(theTask.Parameter) + "\""; info.Domain = Environment.MachineName; info.WorkingDirectory = Path.GetDirectoryName(theTask.Path); info.UseShellExecute = false; info.CreateNoWindow = true; Process batProcess = Process.Start(info); batProcess.WaitForExit();