使用来自 C# 的参数运行批处理文件 [英] Running batch file with arguments from C#

查看:49
本文介绍了使用来自 C# 的参数运行批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的批处理文件

I have a batch file like this

@echo off
xcopy /e %1 %2

我的 C# 代码如下:

I have my C# code as follows:

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# 的参数运行批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆