Console Application Proxy

I’m working on a project where it’s necessary to use a client application provided by a third party to query the third party’s system from within my own system. This can be done very easily in .net by simply redirecting standard input and output.

Here is a sample application that demonstrates how:

var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"WorkApp.exe";
p.Start();

string input;
do
{
    // get request from user
    Console.Write("> ");
    input = Console.ReadLine();
    p.StandardInput.WriteLine(input);

    // get response from console
    var output = p.StandardOutput.ReadLine();
    Console.WriteLine(output);
} while (input != "x");

if (!p.HasExited)
{
    p.Kill();
}
Advertisements

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s