Reputation: 419
I will start from the C++ application to explain my exact requirement. I had a C++ application , which takes an input (a small command called "run") from console (keyboard) with out using any arguments( C++ application is using getchar() in the project). well the problem i am able to run the exe file from C# using System.Diagnostics.Process, but i want to enter the command "run" programitically in C# to execute C++ Application. Is it possible to do that?
Upvotes: 2
Views: 4176
Reputation: 437424
Yes it is. You need to redirect the spawned process's input stream so that you can write to it directly:
var proc = new Process();
proc.StartInfo.FileName = "program.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
var sw = proc.StandardInput;
Now this will write to your process's standard input, just as if you had typed the text using the keyboard:
sw.WriteLine("run something");
Finally, when you are done writing, don't forget to clean up:
sw.Close();
proc.WaitForExit();
proc.Close();
Upvotes: 3
Reputation: 11812
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "run.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j ";
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch
{
// Log error.
}
Upvotes: 0