我有一个像这样的批处理文件
@echo off
xcopy /e %1 %2
我的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();
我希望这会将源文件复制到目标位置。但没有任何反应。我的控制台窗口也没有停留足够的时间,以便我可以看到错误。任何人都可以指导实现这一点。我是批处理文件处理的新手。
修改
在批处理文件的末尾添加pause
。能够重现错误。获取错误
Files not found - Program
直接运行批处理文件可以正常工作。刚刚注意到......当源路径有任何空格时......我收到错误
答案 0 :(得分:10)
如何引用参数?
Arguments = String.Format("\"{0}\" \"{1}\"", _sourcePath, _tempTargetPath) …
答案 1 :(得分:6)
.bat文件是一个文本文件,为了执行它,你应该启动cmd进程。 像这样开始:
System.Diagnostics.Process.Start("cmd.exe", "/c yourbatch.bat");
可能会有其他论点。在没有c#的情况下,在cmd窗口或“运行”对话框中尝试此操作。
答案 2 :(得分:2)
试
string MyBatchFile = @"C:\MybatchFile.bat";
string _sourcePath = @"C:\FolderToCopy\*.*";
string _tempTargetPath = @"C:\TargetFolder\";
即。将*.*
添加到源路径
并将第3行pause
添加到批处理文件
@echo off
copy /e %1 %2
pause