使用C#中的参数运行批处理文件

San*_*ndy 9 c# arguments xcopy batch-file

我有这样的批处理文件

@echo off
xcopy /e %1 %2
Run Code Online (Sandbox Code Playgroud)

我有我的C#代码如下:

string MyBatchFile = @"C:\Program Files (x86)\MybatchFile.bat";
string _sourcePath = @"C:\FolderToCopy";
string _tempTargetPath = @"C:\TargetFolder\";

var process = new Process { 
                   StartInfo = { 
                      Arguments = string.Format("{0} {1}",
                                                _sourcePath,
                                                _tempTargetPath) 
                                } 
                          };
process.StartInfo.FileName = MyBatchFile;
bool b = process.Start();
Run Code Online (Sandbox Code Playgroud)

我希望这会将源文件复制到目标位置.但没有任何反应.我的控制台窗口也没有停留足够的时间,以便我可以看到错误.任何人都可以指导实现这一点.我是批处理文件处理的新手.

编辑

通过pause在批处理文件的末尾添加一个.能够重现错误.得到错误

Files not found - Program
Run Code Online (Sandbox Code Playgroud)

直接运行批处理文件可以正常工作.刚才注意到......当源路径有任何空格......我收到错误

Via*_*nov 12

引用参数怎么样?

Arguments = String.Format("\"{0}\" \"{1}\"", _sourcePath, _tempTargetPath) …
Run Code Online (Sandbox Code Playgroud)


Vla*_*lov 6

.bat文件是一个文本文件,为了执行它,你应该启动cmd进程.像这样开始:

System.Diagnostics.Process.Start("cmd.exe", "/c yourbatch.bat");
Run Code Online (Sandbox Code Playgroud)

可能会有其他论点.在没有c#的情况下,在cmd窗口或"运行"对话框中尝试此操作.