如何检查命令提示符命令执行是否有效?

时间:2021-03-15 11:57:15

标签: c#

所以我想通过使用此代码检查是否使用 c# 安装了 node.js。

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C node -v";
process.StartInfo = startInfo;
process.Start();

我不确定如何检查命令是否成功运行。在 c# 中是否可能,如果是如何实现的?

1 个答案:

答案 0 :(得分:0)

适当地设置 StartInfo 并重定向标准输出。

var proc = new Process 
{
    StartInfo = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        Arguments = "/C node -v",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};

// Starts the process and reads its output.

proc.Start();
string output = proc.StandardOutput.ReadToEnd();