我需要运行批处理文件,它有路径(可以包含空格)作为参数。
批处理文件非常简单:
回声
echo%1> echotest.txt
我用来运行这个批处理文件的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中转换为实际路径,例如:D:\ Test Path \(包含空格)
这对我不起作用,每次都会返回退出代码1。
如果我从info.Arguments中删除\“,它可以工作,但在输出文件中只是D:\ Test
有什么建议吗?
此致
答案 0 :(得分:1)
如果可以的话,你能尝试一下吗?
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();