When I say JavaScript file, I mean the whole file. Not a function. I've seen ways to run a JavaScript function from c# but nothing about running a file.
Asked
Active
Viewed 405 times
1 Answers
0
According to this question: https://stackoverflow.com/a/1469790/13105088, one could run the command with any normal shell.
string strCmdText;
strCmdText= "/C node myscript.js"; // the command to run from the command prompt
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Note that this will show the Command Prompt on Windows.
This following script prevents that.
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 myscript.js";
process.StartInfo = startInfo;
process.Start();
(these example scripts were both provided by the answer I linked to above.)
In addition, you should probably also note:
Important is that the argument begins with /C otherwise it won't work. How Scott Ferguson said: it "Carries out the command specified by the string and then terminates."
Note: this is all assuming you are referring to NodeJS when you are saying a "JavaScript file", but any other interpreter (eg. /C python3 myfile.py
) should also work.

idontknow
- 438
- 5
- 16