Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start NodeJS Application from C# web Form Application

I have built MeteroJS application that I want to start as NodeJS application from C# code.

Here is Windows Form application that is used as control panel for starting and stopping the NodeJS application

enter image description here

I can start NodeJS application manually with command line: (this works!)

set MONGO_URL=mongodb://someAdmin:password@localhost:27017/some_db
set ROOT_URL=http://someservice:8080
set PORT=8080
node bundle\main.js

I want to repeat all action above from command line, this time inside C# application.

This is code that executes on Start button click:

Environment.SetEnvironmentVariable("MONGO_URL", String.Format("mongodb://{0}:{1}@localhost:27017/{2}", usernameTxt.Text, passwordTxt.Text, databaseTxt.Text), EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable("ROOT_URL", String.Format("http://someservice:{0}", portTxt.Text), EnvironmentVariableTarget.Machine);
Environment.SetEnvironmentVariable("PORT", portTxt.Text, EnvironmentVariableTarget.Machine);

Process.Start("CMD.exe", @"/C node bundle\main.js");

I am not sure if this is even possible. This simply does not work and left no logs. Could you please check what I am doing wrong and advise.

like image 834
eomeroff Avatar asked May 12 '26 14:05

eomeroff


2 Answers

Use the following code to execute the node.js cmd

            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.FileName = @"c:\node\node.exe";**//Path to node installed folder****
            string argument = "\\ bundle\main.js";
            p.StartInfo.Arguments = @argument;
            p.Start();
like image 145
Ram Avatar answered May 15 '26 04:05

Ram


This code may help you:

Process p = new Process();    
p.StartInfo.WorkingDirectory= @"C:\Users\USERNAME\Documents\Visual Studio 2015\Projects\Proyecto 1.3\Proyecto 1.3\bin\Debug\server";    
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;    
p.StartInfo.FileName = "cmd.exe";    
p.StartInfo.Arguments = "/c node app.js";    
p.Start();
like image 32
Francisco Estrada Avatar answered May 15 '26 02:05

Francisco Estrada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!