Can you execute another EXE file from within a C# console application? - c#

Can you execute another EXE file from within a C# console application?
Can you pass arguments?
Can you get the exit code back?

Like this:
var proc = new Process();
proc.StartInfo.FileName = "something.exe";
proc.StartInfo.Arguments = "-v -s -a";
proc.Start();
proc.WaitForExit();
var exitCode = proc.ExitCode;
proc.Close();

Related

Running an exe from with another exe C# and closing it

I am trying to run an exe file inside another exe file in C#
That works well, however my problem is the exe that should run inside the other opens another console window, which I need to press "Enter" in, for it to stop after it has done what it does.
Is there a way to do that?
This is the code I have so far.
var proc = new Process();
proc.StartInfo.FileName = "thefile.exe";
proc.Start();
proc.WaitForExit();
var exitCode = proc.ExitCode;
proc.Close();
Thanks
Just use cmd to open and terminat the exe / user window.
string executingCommand;
executingCommand= "/C Path\\To\\Your\\.exe"; // the /C terminates after carrying out the command
System.Diagnostics.Process.Start("CMD.exe", executingCommand);
Are looking for the input redirectioning?
https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput(v=vs.110).aspx
int exitCode = -1;
// Put IDisposable into using
using (var proc = new Process()) {
proc.StartInfo.FileName = "thefile.exe";
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
using (StreamWriter inputWriter = proc.StandardInput) {
inputWriter.Write('\n');
}
proc.WaitForExit();
exitCode = proc.ExitCode;
}

Passing arguments one by one one in console exe by c# code

I want to run an exe file by my c# code. The exe file is a console application written in c#.
The console application performs some actions which includes writing content in database and writing some files to directory.
The console application (exe file) expects some inputs from user.
Like it first asks , 'Do you want to reset database ?' y for yes and n for no.
again if user makes a choice then application again asks , 'do you want to reset files ?'
y for yes and n for no.
If user makes some choice the console application starts to get executed.
Now I want to run this exe console application by my c# code. I am trying like this
string strExePath = "exe path";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = strExePath;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
I want to know how can I provide user inputs to the console application by my c# code?
Please help me out in this. Thanks in advance.
You can redirect input and output streams from your exe file.
See redirectstandardoutput
and redirectstandardinput for examples.
For reading:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
For writing:
...
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
myStreamWriter.WriteLine("y");
...
myStreamWriter.Close();
ProcessStartInfo has a constructor that you can pass arguments to:
public ProcessStartInfo(string fileName, string arguments);
Alternatively, you can set it on it's property:
ProcessStartInfo p = new ProcessStartInfo();
p.Arguments = "some argument";
Here is a sample of how to pass arguments to the *.exe file:
Process p = new Process();
// Redirect the error stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = #"\filepath.exe";
p.StartInfo.Arguments = "{insert arguments here}";
p.Start();
error += (p.StandardError.ReadToEnd());
p.WaitForExit();

Execute a Batch File From C#

I have a small problem. Okay let's say from my C# console application I want to run a batch file that will take an argument. The string variable at the stop of my C# application will be the string argument to pass to the batch file. How would I go about doing it?
Here is my code so far my C# console program:
//String argument to pass to the batch file
string message = "Hello World";
System.Diagnostics.Process process = new System.Diagnostics.Process();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "Greetings.bat";
startInfo.Arguments = "/C " + message;
process.StartInfo = startInfo;
process.Start();
My Batch File
CLS
#ECHO OFF
ECHO %1
You can give argument like this.
ProcessStartInfo psi = new ProcessStartInfo(filePath);
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.Arguments = "value1";

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

Mimic batch file with C#

I have a batch file that runs the four commands
vsinstr -coverage hello.exe
vsperfcmd /start:coverage /output:run.coverage
hello
vsperfcmd /shutdown
How can I use C# to run the four commands?
Add these command to a batch file and use the below code to run it
ProcessStartInfo startInfo;
System.Diagnostics.Process batchExecute;
startInfo = new ProcessStartInfo("batchFilePath");
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
batchExecute = new System.Diagnostics.Process();
batchExecute.StartInfo = startInfo;
batchExecute.Start();
batchExecute.WaitForExit();
Run the commands using Process.Start.
Example
Using the override Process.Start(string fileName, string arguments)
Process.Start("vsinstr", "-coverage hello.exe");
Process.Start("vsperfcmd", "/start:coverage /output:run.coverage");
Process.Start("hello");
Process.Start("vsperfcmd", "/shutdown");
Since you already have a batch file, why not run it from C# instead of running the commands in it from C#? For example:Process.Start

Categories

Resources