【问题标题】:Running Multiple Commands in CMD.EXE from C# program?从 C# 程序在 CMD.EXE 中运行多个命令?
【发布时间】:2020-05-27 06:23:56
【问题描述】:

我正在尝试 1. 运行 cd 命令并 2. 执行 node.js 可执行文件,该可执行文件通过命令窗口接收两个文件名。

以下代码嵌入在 for 循环中,因此文件扩展名变量的变量名发生变化:

command_line = "/C cd C:/Users/esimons/Documents/Software/Serial_GUIC#/ComputerToArduino/images/TraxSecur-Node/TraxSecur-Node/ & node main.js " + image_extension + " " + UID_extension;

System.Diagnostics.Process.Start("CMD.exe", command_line);

我尝试使用在另一个线程上看到的 & 来拆分命令,但它不起作用。

使用替代方法:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = @"C:/Users/esimons/Documents/Software/Serial_GUIC#/ComputerToArduino/images/TraxSecur-Node/TraxSecur-Node/";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine("node main.js " + image_extension + " " + UID_extension);
p.Close();

解决方案: 上面的代码有效!如果您最近安装了 node.js,请重新启动系统

【问题讨论】:

  • 为什么不直接启动节点可执行文件?
  • 我想,/& 这里需要一个空格。另外,您遇到了哪个错误?
  • @PavelAnikhouski 我添加了一个空格,但它仍然没有产生正确的输出。我在此处打开的 (cmd.exe) 与我从 Windows 桌面打开命令提示符并输入上述命令之间有区别吗?
  • @Aybe 我该怎么做?这似乎是一个更简单的解决方案

标签: c# .net cmd command


【解决方案1】:

解决办法:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = @"C:/Users/esimons/Documents/Software/Serial_GUIC#/ComputerToArduino/images/TraxSecur-Node/TraxSecur-Node/";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine("node main.js " + image_extension + " " + UID_extension);
p.Close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多