Kapil
Kapil

Reputation: 1931

how to return the console application data to web application

I build a console application which will prompt for User Name and Password

I have a web application on button click I need to call the console exe to popup the username and password once the user key in the user name and password i need to get the values to pass to my web application

      string filePath = @"D:\\ConsoleApplication\\ConsoleApplication\\ConsoleApplication\\bin\Debug\\ConsoleApplication.exe";
             ProcessStartInfo info = new ProcessStartInfo(filePath, "");
             Process p = Process.Start(info);
            p.StartInfo.UseShellExecute = false;           //don't use the shell to execute the cmd file                                           
            p.StartInfo.RedirectStandardOutput = true;     //redirect the standard output
            p.StartInfo.RedirectStandardError = true;      //redirect standard errors
            p.StartInfo.CreateNoWindow = true;            //don't create a new window
            p.Start();                                   //start the program
            StreamReader sr = p.StandardOutput;         //read the programs output
            string output = sr.ReadToEnd();              //put output into list box
            p.Close();
            p.WaitForExit();

Console Application Code :

  public class Program
    {
        static void Main()
        {
            System.Console.WriteLine("Please enter User Name");
            var userName = Console.ReadLine();
           System.Console.WriteLine("Please enter Password");
           var password = Console.ReadLine();          
        }
    }

I need to get the keyin user name and password values and I need to return back to web page

enter image description here

I tried with above code to get the values from standardoutput but the values are empty

can any one help me..

Upvotes: 2

Views: 1921

Answers (1)

Kapil
Kapil

Reputation: 1931

Below implementation is working for me.

Write to text file and read the values in the web application

Console Code :

 public class Program
    {
        static void Main()
        {
            System.Console.WriteLine("Please enter User Name");
            string userName = Console.ReadLine();
            System.Console.WriteLine("Please enter Password");
            string password = Console.ReadLine();
            var sw = new StreamWriter(Environment.CurrentDirectory + "\\TA.txt");
            sw.Write(userName + "," + password );
            sw.Flush();
            sw.Close();
        }
    }

web Application:

 // Run the console application to key-in the User Name and Password
            string userName = string.Empty; string password = string.Empty;
            TestLog.BeginSection("Step 1: Create New WebAdmin User");
            const string filePath = @"D:\\ConsoleApplication\\ConsoleApplication\\ConsoleApplication\\bin\Debug\\ConsoleApplication.exe";
            var info = new ProcessStartInfo(filePath, "");
            var p = Process.Start(info);
            p.StartInfo.UseShellExecute = false;           //don't use the shell to execute the cmd file                                           
            p.StartInfo.RedirectStandardOutput = true;     //redirect the standard output
            p.StartInfo.RedirectStandardError = true;      //redirect standard errors
            p.StartInfo.CreateNoWindow = true;            //don't create a new window
            p.Start();                                   //start the program
            StreamReader sr = p.StandardOutput;         //read the programs output
            string output = sr.ReadToEnd();             //put output into list box  
            p.Close();
            Thread.Sleep(40000);

            // Read the file as one string.
            var userNamePassword = System.IO.File.ReadAllText(Environment.CurrentDirectory + @"\\TA.txt");
            var strarray = userNamePassword.Split(',');
            if (strarray.Length > 1)
            {
                userName = strarray[0];
                password = strarray[1];
            }
            // Delete the file
            if (File.Exists(Environment.CurrentDirectory + @"\TA.txt"))
            {
                File.Delete(Environment.CurrentDirectory + @"\TA.txt");
            }

You will have the values in username and password parameters.

Happy coding..:)

Upvotes: 1

Related Questions