运行 来自 C# 程序的 CMD.EXE 中的多个命令?

Running Multiple Commands in CMD.EXE from C# program?

我正在尝试 1. 运行 cd 命令和 2. 执行一个 node.js 可执行文件,该可执行文件通过命令 window.

接收两个文件名

以下代码嵌入到 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

,请重新启动系统

解决方案:

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();