【问题标题】:Execute a program from the cmd with arguments使用参数从 cmd 执行程序
【发布时间】:2014-04-21 14:00:39
【问题描述】:

我有一个程序需要从 cmd 运行并带有参数,比如调用的执行文件

程序.exe

我需要从 cmd 中使用 args 运行它,cmd 中的整个命令如下所示:

c:\ram\program.exe /path = c:\program files\NV

如您所见,路径是:“c:\ram\”

执行文件为:“program.exe”

我需要发送的参数是:/path = c:\program files\NV

我该怎么做?

我尝试像这样打开进程:

string strArguments = @"/path = c:\program files\NV";
Process p = new Process();
p.StartInfo.FileName = "program.exe";
p.StartInfo.WorkingDirectory = "c:\\ram\\";
p.StartInfo.Arguments = strArguments;
p.Start();

它不好,我认为问题可能是我没有从 CMD 访问 exe 文件,也许我错了......任何人都知道我该怎么做?

谢谢

【问题讨论】:

  • 代码行 p.StartInfo.WorkingDirectory = "c:\ram\";不起作用。应该是 p.StartInfo.WorkingDirectory = @"c:\ram\";
  • 我想念在我的程序中的消息中写了它:p.StartInfo.WorkingDirectory = "c:\\ram\\"

标签: c# process cmd command-line-arguments


【解决方案1】:

试试这些

p.StartInfo.FileName = "c:\\ram\\program.exe"; 不设置Working Directory

而这个更可能是问题的根源

string strArguments = @"/path = ""c:\program files\NV""";

当路径中有空格时,您必须将整个路径用引号引起来。完整代码如下

string strArguments = @"/path=""c:\program files\NV""";
Process p = new Process();
p.StartInfo.FileName = @"c:\ram\program.exe";
p.StartInfo.Arguments = strArguments;
p.Start();

它应该做你想做的事。

1.运行“cmd.exe”。

2. 转到此目录:“c:\ram\”(当然在 cmd 中)。

3.使用以下参数执行文件“program.exe”:“/path = c\:program files\NV”

它获取 c:\ram\ 文件夹中的 program.exe,并使用带有指定参数的 cmd 执行它。

【讨论】:

  • 我面临的问题是没有让进程“program.exe”执行,我成功执行了程序我的问题是我需要将参数传递给程序,这可能从 cmd 完成,如您所见,我没有根据需要从 cmd 打开 exe,如何打开 cmd 并从 cmd 执行我的 program.exe?
  • 并在 cmd 中找到我需要的路径并传递相关参数?
  • 你英语不太好。我真的不明白你的第一条评论。你正在运行一个应用程序,你想从中运行 cmd 并从该 cmd 运行这个 c:\ram\program.exe /path = c:\program files\NV 命令,对吗?
  • 我改写它,我试图在我的代码中模拟这个动作:1.运行“cmd.exe”。 2.转到此目录:“c:\ram\”(当然在 cmd 中)。 3. 使用以下参数执行文件“program.exe”:“/path = c\:program files\NV”。如何在我的代码中模拟此过程?谢谢!
  • @RonKah 我是这么认为的。试试我的答案中的代码,我包含了完整的代码 sn-p。只需将其复制到您的项目中并尝试一下。
猜你喜欢
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多