I have 2 applications.
One of them is console application, the other is normal form application - both written in C#. I want to open (hidden from view) the console application form the windows form application and be able to send a command lines to the console application.
How can i do that?
You can start the background process
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "Myapplication.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
and after that use the Process.StandardOutput property
// This is the code for the base process
Process myProcess = new Process();
// Start a new instance of this program but specify the 'spawned' version.
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(args[0], "spawn");
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);
myProcess.WaitForExit();
myProcess.Close();
If you want to send commands to this process, just use Process.StandardInput Property
// Start the Sort.exe process with redirected input.
// Use the sort command to sort the input text.
Process myProcess = new Process();
myProcess.StartInfo.FileName = "Sort.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
// Prompt the user for input text lines to sort.
// Write each line to the StandardInput stream of
// the sort command.
String inputText;
int numLines = 0;
do
{
Console.WriteLine("Enter a line of text (or press the Enter key to stop):");
inputText = Console.ReadLine();
if (inputText.Length > 0)
{
numLines ++;
myStreamWriter.WriteLine(inputText);
}
} while (inputText.Length != 0);
One of possible solutions can be IPC, in particularly
NamedPipes
That is already wrapped in .NET 4.0.
Regards.
To start the console application, use the System.Diagnostics.Process class.
To send commands to the console application you need something that is called Interprocess Communication. One way to do it is by using WCF. A simple tutorial can be found here.
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 am trying to execute a file stored under program files. The program is usually called from a command prompt, and output information while it is running. I want to capture the output.
The code below solves half of the job: The program is executed, and the console pops up showing the output in the console, but I cannot capture the output:
ProcessStartInfo start = new ProcessStartInfo();
start.WorkingDirectory = directory;
start.FileName = fileName;
start.Arguments = arguments;
//start.UseShellExecute = false;
//start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
// code
}
Now I am using the code below to capture the output:
ProcessStartInfo start = new ProcessStartInfo();
start.WorkingDirectory = directory;
start.FileName = fileName;
start.Arguments = arguments;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
The program crashes in the line where I execute the start command with the error "The system cannot find the file specified"
What I don't get is, the first part of the program works, but not the second.
I have search a lot on this topic, and there are many suggestions but have not been able to find one which solves my issue.
I am using .NET 3.5 for the program which I prefers due to compatibility with other parts of the program.
There is a process in my case which starts another process and subscribes to the Exited event.
The second process at the end should pass an error string somehow to the first process. The first process will display the message on the screen.
How to pass a string at the end of application in such a way that another app would be able to read the string and display it?
You can redirect the StandardError stream: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("net ","use "+ args[0]);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardError;
// Read the standard error of net.exe and write it on to console.
Console.WriteLine( myStreamReader.ReadLine());
myProcess.Close();
Research the following, and pick the one that suites your needs best for your scenario. My guess is that you either need Output or Error if you're passing data back to the parent process.
StartInfo.RedirectStandardOutput
StartInfo.RedirectStandardError
StartInfo.RedirectStandardInput
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();
I've some troubles with running processes and passing args to them.
I know how to run process with some args
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", "/c something");
Process p = Process.Start(psi)
The problem is that after script is executed process is terminated. That's why there is "/c"
But I'm running multiple scripts and I would like to run them in one process ("cmd.exe") not to start new process every time.
Is there some solutions for it ?
I hope somebody understand what I'm talking about ;)
I recommend you utilize a batch file to script the execution of your executables and call your batch file instead. Or, you can do this -
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = new StreamWriter(p.StandardInput))
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("mysql -u root -p");
sw.WriteLine("mypassword");
sw.WriteLine("use mydb;");
}
}
It sounds like you ought to investigate redirecting the standard input - be sure to also set psi.UseShellExecute to false. You'll probably also want to redirect standard output, so you can have some way of knowing what your child process is doing.
Read more about redirection here.