-1

So I want to check if node.js is installed using c# by using this code.

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

I'm not sure how to check if the command ran successfully. Is it possible in c# and if it is how?

Mr Meowzz
  • 1
  • 2
  • You may also want to check the `ExitCode`, in case the error was handled by the process and it returned something other than zero. This of course depends 100% on the process following the exit code guidelines. – Andrew Mar 15 '21 at 12:04
  • Redirect its standard output (or error), and read all its output, wait for it to exit, parse it, and then you will know. If it's interactive, or pumps to both streams and you need to react as it runs, hook the DataReceived events of the process and track what it outputs as it runs – Caius Jard Mar 15 '21 at 12:05
  • 2
    95% of process launch issues are caused by launching cmd.exe **instead of the process you are interested in**. This question is no exception. – mjwills Mar 15 '21 at 12:05

1 Answers1

0

Set StartInfo appropiately and redirects the standard output.

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();
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56