从C#Web表单应用程序启动NodeJS应用程序

时间:2016-02-10 10:08:43

标签: c# node.js winforms meteor command-line

我已经构建了MeteroJS应用程序,我希望从C#代码开始作为NodeJS应用程序。

这是Windows窗体应用程序,用作启动和停止NodeJS应用程序的控制面板

enter image description here

我可以使用命令行手动启动NodeJS应用程序:(这有效!)

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

我想从命令行重复上面的所有操作,这次是在C#应用程序中。

这是在“开始”按钮上执行的代码:

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

我不确定这是否可行。这根本不起作用,没有留下任何记录。 你能否检查一下我做错了什么并提出建议。

2 个答案:

答案 0 :(得分:2)

使用以下代码执行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();

答案 1 :(得分:1)

此代码可以为您提供帮助:

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