2

I have a .Net core 3.0 application that needs to start another Node.js application through the command line.
How do I do this?

C:\NodeApp>node app.js

Very thanks!

Manth
  • 31
  • 1
  • You need to use `Process.Start` to launch the "node" application with required arguments: [How do I start a process from C#?](https://stackoverflow.com/questions/181719/how-do-i-start-a-process-from-c) – Mohammad Dehghan Apr 11 '20 at 10:53
  • Does this answer your question? [Can a C# application communicate with Node.js code?](https://stackoverflow.com/questions/15136952/can-a-c-sharp-application-communicate-with-node-js-code) – JeremyTCD Apr 11 '20 at 11:11

2 Answers2

2

Try this

using System.Diagnostics;

Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C node <path>";
// /C is important
process.Start();

Try using the full path to the file

Nekuskus
  • 143
  • 1
  • 11
dommilosz
  • 127
  • 13
1

A better solution than dommilosz's is to just run node.exe from its full path (or include it in your project, that's a bit advanced but if you want you can see this question Embedding an external executable inside a C# program). After that set the process.StartInfo.Arguments property to the relative or full path of your node.js script. If you wish you can also redirect standard input, error and output of the script by setting these properties of Process.StartInfo to true, for more intormation about this view the "Properties" section here https://learn.microsoft.com/en-gb/dotnet/api/system.diagnostics.processstartinfo?view=netcore-3.1

Nekuskus
  • 143
  • 1
  • 11