【问题标题】:C# Passing Multiple Arguments to BAT FileC# 将多个参数传递给 BAT 文件
【发布时间】:2011-08-23 20:32:18
【问题描述】:

我有这个 BAT 文件“iARP.BAT”

Content Begin
@ECHO OFF
npg -vv -f %1 -d %2
Content End

我正在尝试将文件名(在循环中)作为第一个参数传递,将设备名称(先前声明的变量)作为第二个参数传递。我正在尝试这样做:

for (int i = 1; i < arpFiles.Count; i++) {

p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.WorkingDirectory = Application.StartupPath;
p.StartInfo.FileName = Application.StartupPath + "\\iARP.bat";
String argument1 = Application.StartupPath + "\\" + arpFiles[0].Name;
p.StartInfo.Arguments = argument1 + deviceName;
p.StartInfo.Verb = "runas";
p.StartInfo.CreateNoWindow = true;
p.Start();
}

顺便说一句 arpFiles = 列表 但它没有执行任何人都可以帮助我吗?

【问题讨论】:

  • 我建议添加这一行:ProcessStartInfo si = p.StartInfo,并将您的代码更改为:si.UseShellExecute = false; 等。或者只是创建一个新的ProcessStartInfo 结构并使用初始化语法:p.StartInfo = new ProcessStartInfo() { UseShellExecute = true, /* etc */ };。少重复:)
  • 为什么调用一个除了运行另一个命令什么都不做的批处理文件?为什么不直接运行命令?

标签: c# .net batch-file command-line-arguments process.start


【解决方案1】:

您需要在Arguments 属性中指定它们:

p.StartInfo.Arguments = string.Format("{0} {1}", argument1, argument2);

【讨论】:

  • 我添加了这一行并打印出控制台输出,它说找不到文件,但我可以看到它存在。我认为问题可能是文件路径太长,因为在错误消息中,整个文件路径没有显示它被截断
  • @GSUgambit - 这是可能的。 FileName 应该指向批处理文件的位置,Arguments 应该只是传入的参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
相关资源
最近更新 更多