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
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.
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();
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();
Related
I have a command line application that calls a 'netsh' process and changes some IP information. The problem that I have is that every time I call my app in CMD or PowerShell, it starts a new instance of CMD(opens a new CMD window and closes it after it's finished executing)
I would like to know if there is a way to have everything happen in the same window
here is the code that starts a process:
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("netsh", queryStr);
p.StartInfo = psi;
p.Start();
Console.WriteLine("netsh query string is: " + "***" + queryStr + "***");
This is my first question, please don't judge too harshly
I hope I understood your question correctly. You could hide the Shell, while redirecting the ouput. For example,
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo("netsh", queryStr);
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
p.StartInfo = psi;
p.Start();
var output = p.StandardOutput.ReadToEnd();
ProcessStartInfo.WindowStyle allows to set the state of Window when the process starts. In the current scenario, you could set it to ProcessWindowStyle.Hidden
ProcessStartInfo.UseShellExecute indicates whether to use the OS's Shell. Disabling this would help in redirecting the output.
ProcessStartInfo.RedirectStandardOutput indicates whether the output is written to StandardOutput. By setting it to true, you can redirect the output stream and use the Process.StandardOutput to read the output and display as per application design
I'm trying to launch a command in a console window / I'm using a gtk form/
So I've tried to launch it this way:
Process p = new Process ();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "bash";
p.StartInfo.CreateNoWindow = false;
p.StartInfo.Arguments ="/tmp/test.sh";
p.Start ();
p.WaitForExit ();
but it won't show any thing.
for those who only use windows, it's something like:
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments =" c:\\test.bat";
I've tried to change UseShellExecute to true but the problem still exist..
Any Ideas??
Bash runs the script but if you want to see output, you need to run it in a terminal.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "gnome-terminal"; // Replace with whichever terminal you want to use
p.StartInfo.CreateNoWindow = false;
p.StartInfo.Arguments ="-x bash /tmp/test.sh";
//p.StartInfo.Arguments ="-e \"bash -c /tmp/test.sh;bash\""; // Use this if you want the terminal window to stay open
p.Start();
p.WaitForExit();
I am trying to run a python code from my UI. i am using the code below to do so,
Process p = new Process();
string cmd = #"python filepath & exit";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.RedirectStandardInput = true;
p.Start();
StreamWriter myStreamWriter = p.StandardInput;
myStreamWriter.WriteLine(cmd.ToString());
myStreamWriter.Close();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
the above code opens a cmd prompt and executes the python file.
this code runs fine when i test it using a console application but it doesn't work when i use it within the event function for "run" button in my UI application. Is there any particular reason for it?
I have a C# code that uses command prompt to call a python script. The script currently takes about 25 seconds to run. When run in command prompt, the python script has several outputs until finally outputting "done". My C# code runs the script, but never closes the command prompt if I use "WaitForExit". So my thought is I need some kind of logic to check to see if "done" has been outputted, but the internet has not been very helpful with methodology.
Here's what I have, it currently only outputs the first line, "Microsoft Windows [Version 6.1.7601]". Obviously, no good.
var p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = #"C:\Optimization";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(#"ipy spot_for_labview.py");
StreamReader mySR = p.StandardOutput;
string mystr = mySR.ReadLine();
Debug.WriteLine(mystr);
p.WaitForExit(25000); //25000 is a placeholder until better method found.
p.Close();
If there's anyway to close the process after it finishes, or to get all the cmd output I'm all ears.
Any help is appreciated.
did you try this event ? Process.OutputDataReceived Event
or Process.ErrorDataReceived Event
here is the code from MSDN
Process sortProcess;
sortProcess = new Process();
sortProcess.StartInfo.FileName = "Sort.exe";
sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
...
....
...
private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
}
I want to run python code from C# through command Prompt.The Code is attached below
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = #"d:";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine(#"cd D:\python-source\mypgms");
p.StandardInput.WriteLine(#"main.py -i example-8.xml -o output-8.xml");
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine("Output:");
Console.WriteLine(output);
Output :
D:\python-source\mypgms>main.py -i example-8.xml -o output-8.xml
D:\python-source\mypgms>
But nothing happened.Actually main.py is my main program and it takes 2 arguments. one is input xml file and another one is converted output xml file.
But i dont know how to run this python script from C# through command prompt. Please Guide me to get out of this issue...
Thanks & Regards,
P.SARAVANAN
I think you are mistaken in executing cmd.exe. I'd say you should be executing python.exe, or perhaps executing main.py with UseShellExecute set to true.
At the moment, your code blocks at p.WaitForExit() because cmd.exe is waiting for your input. You would need to type exit to make cmd.exe terminate. You could add this to your code:
p.StandardInput.WriteLine(#"exit");
But I would just cut out cmd.exe altogether and call python.exe directly. So far as I can see, cmd.exe is just adding extra complexity for absolutely no benefit.
I think you need something along these lines:
var p = new Process();
p.StartInfo.FileName = #"Python.exe";
p.StartInfo.Arguments = "main.py input.xml output.xml";
p.StartInfo.WorkingDirectory = #"D:\python-source \mypgms";
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
Also the Python script appears to output to a file rather than to stdout. So when you do p.StandardOutput.ReadToEnd() there will be nothing there.
Why not host IronPython in your app and then execute the script?
http://blogs.msdn.com/b/charlie/archive/2009/10/25/hosting-ironpython-in-a-c-4-0-program.aspx
http://www.codeproject.com/Articles/53611/Embedding-IronPython-in-a-C-Application
or use py2exe to pragmatically convert your python script to exe program.
detail steps...
download and install py2exe.
put your main.py input.xml and output.xml in c:\temp\
create setup.py and put it in folder above too
setup.py should contain...
from distutils.core import setup
import py2exe
setup(console=['main.py'])
your c# code then can be...
var proc = new Process();
proc.StartInfo.FileName = #"Python.exe";
proc.StartInfo.Arguments = #"setup.py py2exe";
proc.StartInfo.WorkingDirectory = #"C:\temp\";
proc.Start();
proc.WaitForExit();
proc.StartInfo.FileName = #"C:\temp\dist\main.exe";
proc.StartInfo.Arguments = "input.xml output.xml";
proc.Start();
proc.WaitForExit();