本文介绍了使用C#中的参数运行批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的批处理文件
I have a batch file like this
@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();
我希望这会将源文件复制到目标位置.但是什么也没发生.我的控制台窗口也没有停留足够的时间,因此我可以看到该错误.任何人都可以指导实现这一目标.我是批处理文件的新手.
I expect this to copy the source files to target location. But nothing happens. My console window also does not stay for enough time so that I can see the error. Can anyone guide to achieve this. I am new in batch files processing.
修改
通过在批处理文件的末尾添加pause
.能够重现错误.错误提示为
By adding a pause
in the end of batch file. Able to reproduce error. Getting error as
Files not found - Program
直接运行批处理文件可以正常工作.刚注意到...当源路径中有空格时......我收到错误消息
Running batch file directly does work fine. Just now noticed......when source path has any spaces....I am getting error
推荐答案
引用参数怎么样?
Arguments = String.Format("\"{0}\" \"{1}\"", _sourcePath, _tempTargetPath) …
这篇关于使用C#中的参数运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!