none
Auto enter text into another console application from the current console application. RRS feed

  • Question

  • Hi All,

    I have an existing third party console application (App 1) that can not be controlled by me, and it needs an user input before it proceed. Now I created another console application (App 2) that uses Process object to startup that App 1, since App 1 needs an user input before it proceeds, so i send a standard input from App 2 to App 1, but i keep getting errors: App 1 has encounter a problem and needs to close. we are sorry for any inconvenience.

    To mimic the scenaro, i manually created a console application that requires an user input before it proceeds, below is the code for App 1:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Principal;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                WindowsIdentity user = WindowsIdentity.GetCurrent();
                WindowsPrincipal principle = new WindowsPrincipal(user);
                if (principle.IsInRole(WindowsBuiltInRole.User) == true)
                {
                    Console.WriteLine("User");
                }
                if (principle.IsInRole(WindowsBuiltInRole.PowerUser) == true)
                {
                    Console.Write("please enter y or n to continue...");
                    ConsoleKeyInfo keyInfo = Console.ReadKey();
                    if (keyInfo.Key.ToString().ToUpper() == ConsoleKey.Y.ToString())
                    {
                        Console.WriteLine("\nY key pressed: PowerUSer");
                    }
                    else if (keyInfo.Key.ToString().ToUpper() == ConsoleKey.N.ToString())
                    {
                        Console.WriteLine("\nN key pressed: PowerUSer");
                    }
                    else
                    {
                        Console.WriteLine("\nInvalid key pressed!");
                    }
                }
            }
        }
    }

    and this is the code for App 2 that triggers App 1 and sends an user input to App 1:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Diagnostics;
    using System.Text;
    namespace AutoAcceptUserInput
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (Process process = new Process())
                {
                    string command = @"C:\ConsoleApplication5\bin\Release\ConsoleApplication1.exe";
                    string argument = "";
                    ProcessStartInfo psi = new ProcessStartInfo(command, argument);
                    process.StartInfo = psi;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.ErrorDialog = false;
                    process.StartInfo.RedirectStandardOutput = false;
                    process.StartInfo.RedirectStandardInput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.Start();
                    process.StandardInput.Write("y");
                    process.StandardInput.Close();
                    process.WaitForExit();
                }
            }
        }
    }

    there is no problems at all when i run App 1 alone, could someone in here shed me a light, much appreciated.

    Tuesday, August 19, 2014 7:09 AM

Answers

  • After debugging your application you may find exception message (you should try debugging it by yourself):

    Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.

    You cannot use Console.ReadKey because child process does not own Console. You may find solution reading this article.

    I converted your App1 a bit so the error message disappear:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Principal;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                WindowsIdentity user = WindowsIdentity.GetCurrent();
                WindowsPrincipal principle = new WindowsPrincipal(user);
                if (principle.IsInRole(WindowsBuiltInRole.User) == true)
                {
                    Console.WriteLine("User");
                }
                if (principle.IsInRole(WindowsBuiltInRole.PowerUser) == true)
                {
                    Console.Write("please enter y or n to continue...");
                    char keyInfo = (char)Console.Read();
                    if (keyInfo.ToString().ToUpper() == ConsoleKey.Y.ToString())
                    {
                        Console.WriteLine("\nY key pressed: PowerUSer");
                    }
                    else if (keyInfo.ToString().ToUpper() == ConsoleKey.N.ToString())
                    {
                        Console.WriteLine("\nN key pressed: PowerUSer");
                    }
                    else
                    {
                        Console.WriteLine("\nInvalid key pressed!");
                    }
                }
            }
        }
    }

    • Edited by Mr. Zator Tuesday, August 19, 2014 7:58 AM
    • Proposed as answer by Mr. Zator Tuesday, August 19, 2014 12:25 PM
    • Marked as answer by AndieDu Wednesday, August 20, 2014 7:36 AM
    • Unmarked as answer by AndieDu Monday, August 25, 2014 11:14 AM
    • Marked as answer by AndieDu Tuesday, August 26, 2014 6:31 AM
    Tuesday, August 19, 2014 7:51 AM

All replies

  • After debugging your application you may find exception message (you should try debugging it by yourself):

    Cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console.Read.

    You cannot use Console.ReadKey because child process does not own Console. You may find solution reading this article.

    I converted your App1 a bit so the error message disappear:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Principal;
    using System.Text;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                WindowsIdentity user = WindowsIdentity.GetCurrent();
                WindowsPrincipal principle = new WindowsPrincipal(user);
                if (principle.IsInRole(WindowsBuiltInRole.User) == true)
                {
                    Console.WriteLine("User");
                }
                if (principle.IsInRole(WindowsBuiltInRole.PowerUser) == true)
                {
                    Console.Write("please enter y or n to continue...");
                    char keyInfo = (char)Console.Read();
                    if (keyInfo.ToString().ToUpper() == ConsoleKey.Y.ToString())
                    {
                        Console.WriteLine("\nY key pressed: PowerUSer");
                    }
                    else if (keyInfo.ToString().ToUpper() == ConsoleKey.N.ToString())
                    {
                        Console.WriteLine("\nN key pressed: PowerUSer");
                    }
                    else
                    {
                        Console.WriteLine("\nInvalid key pressed!");
                    }
                }
            }
        }
    }

    • Edited by Mr. Zator Tuesday, August 19, 2014 7:58 AM
    • Proposed as answer by Mr. Zator Tuesday, August 19, 2014 12:25 PM
    • Marked as answer by AndieDu Wednesday, August 20, 2014 7:36 AM
    • Unmarked as answer by AndieDu Monday, August 25, 2014 11:14 AM
    • Marked as answer by AndieDu Tuesday, August 26, 2014 6:31 AM
    Tuesday, August 19, 2014 7:51 AM
  • great effort, thanks a lot for it. that was working like a shining star.

    but as i as saying before, i do not have control over the third party applicaton, in which will be triggered by my application. hopefully that third party app is using the Console.Read() rather than Console.ReadKey().

    anyways, thanks a lot for your help.

    Wednesday, August 20, 2014 7:36 AM
  • as previous discussed, i do not have control of that third party app, now "process.StandardInput.Write("y");" did not work as that third party is still waiting for an user interraction.

    probably i m a bit too fussy here, but would could be the better way to send a value to other applications other than process.StandardInput.Write("y"); probbably need to send a "Enter" char into that application?

    Monday, August 25, 2014 11:13 AM
  • Honestly i do not know a better way than StandardInput stream. If your third party app does not get the input try using Flush method after sending data.

    If this will not work you are probably sending data in wrong format. You are asking whether send "Enter" at the end of string? It is a matter of this third party app and how does it parse data. If you do not know it you should try sending data in different formats until it will start working as you want.

    Monday, August 25, 2014 12:05 PM
  • Found a third party specific paramater to bypass user interraction, so all good and fully automated. thanks all for help.

    Tuesday, August 26, 2014 6:33 AM