如何将控制台应用程序数据返回到Web应用程序

时间:2016-05-18 07:00:29

标签: c#-4.0 web-applications console console-application console.log

我构建了一个控制台应用程序,它将提示用户名和密码

我有一个按钮点击的Web应用程序我需要调用控制台exe来弹出用户名和密码一旦用户输入用户名和密码我需要将值传递给我的Web应用程序

      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();

控制台应用程序代码:

  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();          
        }
    }

我需要获取keyin用户名和密码值,我需要返回到网页

enter image description here

我尝试使用上面的代码从standardoutput获取值,但值为空

任何人都可以帮助我..

1 个答案:

答案 0 :(得分:0)

以下实施对我有用。

写入文本文件并阅读Web应用程序中的值

控制台代码:

 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();
        }
    }

网络应用程序:

 // 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");
            }

您将拥有用户名和密码参数中的值。

快乐编码.. :)

相关问题