【问题标题】:how to return the console application data to web application如何将控制台应用程序数据返回到 Web 应用程序
【发布时间】:2016-05-18 07:00:29
【问题描述】:

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

我有一个单击按钮的 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用户名和密码值,我需要返回网页

我尝试使用上面的代码从标准输出中获取值,但值是空的

谁能帮帮我..

【问题讨论】:

  • 在你的 main() 调用 Process.Start("IEXPLORE.EXE", "-nomerge youresite.com?username=Kapil&password=Kapil"); 它将打开一个带有给定 URL 和查询字符串的 IE。
  • 我需要获取用户键入的动态值。
  • 是的,只需将硬编码的 Kapil 分别替换为用户名变量和密码变量即可。
  • 如果我这样做,我要求在类库 proj 中运行控制台并且无法获取查询字符串。任何其他解决方案。

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


【解决方案1】:

下面的实现对我有用。

写入文本文件并读取 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");
            }

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

编码愉快..:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多