Redirect console output to textbox in separate program

前端 未结 5 2062
误落风尘
误落风尘 2020-11-22 16:16

I\'m developing an Windows Forms application that requires me to call a separate program to perform a task. The program is a console application and I need to redirect stan

5条回答
  •  天命终不由人
    2020-11-22 16:53

    You can use the following code

            MemoryStream mem = new MemoryStream(1000);
            StreamWriter writer = new StreamWriter(mem);
            Console.SetOut(writer);
    
            Assembly assembly = Assembly.LoadFrom(@"C:\ConsoleApp.exe");
            assembly.EntryPoint.Invoke(null, null);
            writer.Close();
    
            string s = Encoding.Default.GetString(mem.ToArray());
            mem.Close();
    

提交回复
热议问题