简体   繁体   English

从 C# 运行 CMD 命令

[英]Running a CMD command from C#

i am trying to run the following cmd command, that works on the CMD, from a C# console app but nothing happens:我正在尝试从 C# 控制台应用程序运行以下 cmd 命令,该命令适用于 CMD,但没有任何反应:

      string strCmdText = "\\office\\Public\\Tools\\myTool\\myTool_V1.0\\myTool.exe -kan tools -kdb Adhoc - ktn Components3 - uri https://coprime.osdinfra.net";
      System.Diagnostics.Process.Start("CMD.exe", strCmdText);

the cmd window is emmidiatly closed after pressing F5 in VS so i can't see the output of "myTool.exe" - which does in fact print status about its progress on the cmd when run from a cmd window.在 VS 中按 F5 后,cmd 窗口被 emmidiatly 关闭,因此我看不到“myTool.exe”的输出 - 实际上,当从 cmd 窗口运行时,它确实打印了有关其在 cmd 上的进度的状态。

Also the desired effect of the program doesn't happen so i know it didn't work.该程序的预期效果也没有发生,所以我知道它没有用。

Need help please需要帮助请

Command Prompt does not take a program in as an argument to start.命令提示符不会将程序作为启动参数。 However, I can't see a reason to use command prompt here.但是,我看不到在这里使用命令提示符的理由。 You're code is starting the process "Cmd.exe" so it can start another process.您的代码正在启动进程“Cmd.exe”,因此它可以启动另一个进程。 Why not eliminate the middle man and just start the process you want to start?为什么不去掉中间人,直接开始你想开始的过程? Then you can pass the process' real arguments as arguments in process.start().然后,您可以将进程的实际参数作为 process.start() 中的参数传递。

Update:更新:

You can start a program from command prompt, but it's a specific command.您可以从命令提示符启动程序,但它是一个特定的命令。 It goes like this:它是这样的:

CMD.exe /c {string of commands to execute} CMD.exe /c {要执行的命令串}

So for instance, you could run it through cmd if you need to by doing this:因此,例如,如果需要,您可以通过 cmd 运行它:

  string strCmdText = "/c start \\office\\Public\\Tools\\myTool\\myTool_V1.0\\myTool.exe -kan tools -kdb Adhoc - ktn Components3 - uri https://coprime.osdinfra.net";
  System.Diagnostics.Process.Start("CMD.exe", strCmdText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM