How to show console when running a process? - c#

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

Related

C# Call EXE from console app

I have a console app that does a number of things. One of the steps involves calling an EXE (AzCopy). I've successfully run this type command in a prompt and it looks like this:
AzCopy /Source:https://mysite.table.core.windows.net/MYTABLE/ /SourceKey:SOURCEKEY /Dest:C:\export /Manifest:MYTABLE
I need to run this script in the middle of a console app's execution. I've seen a number of different ways to do this, but none seem to work. I've tried this:
System.Diagnostics.Process.Start("CMD.exe", command);
And this:
var procStartInfo = new ProcessStartInfo("AzCopy", command);
And this:
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = command;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
None seem to actually kick off the AzCopy call. What gives?
System.Diagnostics.Process.Start("CMD.exe", command); should work.
But you need to add /C at the beginning of your command.
Something like this:
"/C AzCopy /Source:https://mysite.t........................................."

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

Showing output of a running cmd process

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)
{
}

Is this possible to run a python code from C# through command prompt?

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

How to launch an external process in C#, using a cmd window

I am having trouble executing an external console application using Process.Start
Process p = new Process();
p.StartInfo.WorkingDirectory = "dump";
p.StartInfo.FileName = "test.exe";
p.StartInfo.Arguments = s;
p.Start();
When the argument that p generates executes, the external application crashes, although if I copy the exact same argument in a command line window it runs fine.
So my question instead how would I create a new instance of a command window and then add the command test.exe + s to run?
So effectively I am launching cmd and then adding my arguments on to it
If you want to run test.exe prm1 prm2 via cmd, use cmd.exe /c test.exe prm1 prm2. Though I don't really understand what this has to do with the crashes. Sounds like your problem is with test.exe - find out what's causing it to crash, and that will help you fix your C# code so that you don't need the cmd.
One of the places I would examine is the working directory. When you set it to "dump", are you sure the current directory is what you expect? Try using a full path first. It's possible that test.exe happens to be in the system path so it gets executed, but its working directory is not what it expects, and this causes it to crash.
try this:
ProcessStartInfo processToRunInfo = new ProcessStartInfo();
processToRunInfo.Arguments = "Arguments");
processToRunInfo.CreateNoWindow = true;
processToRunInfo.WorkingDirectory = "C:\\yourDir\\";
processToRunInfo.FileName = "test.exe";
//processToRunInfo.CreateNoWindow = true;
//processToRunInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = new Process();
process.StartInfo = processToRunInfo;
process.Start();
Process p = new Process();
p.StartInfo.WorkingDirectory = "/full/path/to/dump";
p.StartInfo.FileName = "/full/path/to/test.exe";
p.StartInfo.Arguments = s; // will call 'text.exe s'
p.Start();
Take a look at MSDN.
You need to Create an instance of the StartInfo class and user Start() such as
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "www.example.com";
Process.Start(startInfo);
Try it!
Rewriting your code would look something like this:
ProcessStartInfo startInfo = new ProcessStartInfo("test.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
startInfo.WorkingDirectory = "dump";
startInfo.Arguments = "s";
Process.Start(startInfo);

Categories

Resources