C# form程序读取console 程序的控制台信息

     这阶段遇到这么一个需求,wpf的桌面应用程序调用一个consloe控制台程序,且要读取控制台上的信息。
     暂且做了一个debug,通过 Process 类的StandardOutput以及RedirectStandardOutput属性完成所需要求。


     console程序代码:
 
    static void Main(string[] args)
        {
            Console.WriteLine("debug");
            Console.ReadLine();
            Console.WriteLine("debug");
            Console.ReadLine();
            Console.WriteLine("debug");
            Console.ReadLine();
        }

 console程序执行编译生成“ConsoleAppTest.exe“

form的应用程序代码:

               Process process = new Process();

                string strArguments = "";

                process.StartInfo.FileName = @"F:\fenglifeng\codetest\ConsoleAppTest\ConsoleAppTest\bin\Debug\ConsoleAppTest.exe";
                process.StartInfo.Arguments = strArguments;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardError = true;

                process.Start();

                //p.StartInfo.RedirectStandardOutput = true;
                //p.StartInfo.UseShellExecute = false;
                StreamReader sr = process.StandardOutput;
                StreamWriter sw = process.StandardInput;
                MessageBox.Show(sr.ReadLine());
                sw.WriteLine("12");
                MessageBox.Show(sr.ReadLine());
                sw.WriteLine("12");
                MessageBox.Show(sr.ReadLine());
                sw.WriteLine("12");
在以上代码中可以得出,APP程序通过process类启动console程序,再通过重定向console程序的std进出口,实现form程序与console程序控制台之间的通信。







猜你喜欢

转载自blog.csdn.net/fenglifeng1987/article/details/48733391
今日推荐