使用C#执行命令行 [英] Execute command line using C#

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

问题描述

所有我想要做的就是发送打开与原本应该超级简单的Program.exe文件
模型的命令

All I am trying to do is send a command that opens a model with the program.exe Supposed to be super simple!

例如:

"C:\Program Files (x86)\River Logic\Enterprise Optimizer 7.4 Developer\EO74.exe" "C:\PauloXLS\Constraint Sets_1.cor"

该线之上运行良好,如果上粘贴命令提示符窗口。
但是,试图通过对我的代码完全相同的字符串时,它被卡住C:\Program

The line above works well if pasted on the command prompt window. However, when trying to pass the same exact string on my code it gets stuck on C:\Program

string EXE = "\"" + @tbx_base_exe.Text.Trim() + "\"";
string Model = "\"" + @mdl_path.Trim()+ "\"";

string ExeModel = EXE + " " + Model;

MessageBox.Show(ExeModel);

ExecuteCommand(ExeModel);



ExeModel正显示出以下基于Visual Studio系列TE:

ExeModel is showing te following line on Visual Studio:

"\"C:\\Program Files (x86)\\River Logic\\Enterprise Optimizer 7.4 Developer\\EO74.exe\" \"C:\\PauloXLS\\Constraint Sets_1.cor\""

要我看起来是我需要发送到以下方法的字符串:

To me looks like it is the string I need to send in to the following method:

public int ExecuteCommand(string Command)
{
   int ExitCode;
   ProcessStartInfo ProcessInfo;
   Process Process;

   ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command);
   ProcessInfo.CreateNoWindow = true;
   ProcessInfo.UseShellExecute = true;

   Process = Process.Start(ProcessInfo);
   Process.WaitForExit();
   ExitCode = Process.ExitCode;
   Process.Close();

   return ExitCode;
}



事情我已经试过:

Things I've tried:


  1. 在一个时间仅传递一个命令(按预期),但由于模型文件不是一个选项将与软件的另一个版本打开。

  2. 试过修剪

  3. 试图用@与\

任何人都可以看到任何明显的错误?谢谢。

Can anyone see any obvious mistake? Thanks.

推荐答案

你为什么要打开一个命令提示符(cmd.exe的)?你只需要把这个可执行文件的名称。

Why are you opening a command prompt (cmd.exe)? Just pass the name of the executable.

这篇关于使用C#执行命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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