Monday, 23 October 2017

Capturing console stream input for processing


Some good articles below found interesting in the web.

The simplest

The sample Host and Client implementation here

My Own test


 class Program
 {
  static public void test()
  {
   System.Diagnostics.Process cmd = new System.Diagnostics.Process();
   cmd.StartInfo.FileName = "cmd.exe";
   cmd.StartInfo.RedirectStandardInput = true;
   cmd.StartInfo.RedirectStandardOutput = true;
   cmd.StartInfo.CreateNoWindow = true;
   cmd.StartInfo.UseShellExecute = false;
   cmd.Start();
   cmd.StandardInput.Flush();

   cmd.StandardInput.WriteLine("echo 5 > input.txt");
   cmd.StandardInput.WriteLine("echo 3 1 2 3 >> input.txt");
   cmd.StandardInput.WriteLine("echo 2 1 1 >> input.txt");
   cmd.StandardInput.WriteLine("echo 4 10 5 5 1 >> input.txt");
   cmd.StandardInput.WriteLine("echo 0 >> input.txt");
   cmd.StandardInput.WriteLine("echo 1 2 >> input.txt");
   cmd.StandardInput.WriteLine("PrizeDraw < input.txt");
   cmd.StandardInput.Flush();
   cmd.StandardInput.Close();
   string line;
   int i = 0;
   do
   {
    line = cmd.StandardOutput.ReadLine();
    i++;
    if (line != null)
    {
     Console.WriteLine(line);
     //Console.WriteLine("Line " + i.ToString() + " -- " + line);
    }
   } while (line != null);
  }
  static void Main(string[] args)
  {
   test();
   Console.ReadLine();
  }
 }