<legend id='uWpMD'><style id='uWpMD'><dir id='uWpMD'><q id='uWpMD'></q></dir></style></legend>

    1. <small id='uWpMD'></small><noframes id='uWpMD'>

        <i id='uWpMD'><tr id='uWpMD'><dt id='uWpMD'><q id='uWpMD'><span id='uWpMD'><b id='uWpMD'><form id='uWpMD'><ins id='uWpMD'></ins><ul id='uWpMD'></ul><sub id='uWpMD'></sub></form><legend id='uWpMD'></legend><bdo id='uWpMD'><pre id='uWpMD'><center id='uWpMD'></center></pre></bdo></b><th id='uWpMD'></th></span></q></dt></tr></i><div id='uWpMD'><tfoot id='uWpMD'></tfoot><dl id='uWpMD'><fieldset id='uWpMD'></fieldset></dl></div>
          <bdo id='uWpMD'></bdo><ul id='uWpMD'></ul>
        <tfoot id='uWpMD'></tfoot>

      1. 将 Windows 应用程序中的 console.writeline 重定向到字

        时间:2023-08-26

              <tbody id='FedGe'></tbody>
            • <small id='FedGe'></small><noframes id='FedGe'>

            • <tfoot id='FedGe'></tfoot>
                  <bdo id='FedGe'></bdo><ul id='FedGe'></ul>

                  <legend id='FedGe'><style id='FedGe'><dir id='FedGe'><q id='FedGe'></q></dir></style></legend>
                • <i id='FedGe'><tr id='FedGe'><dt id='FedGe'><q id='FedGe'><span id='FedGe'><b id='FedGe'><form id='FedGe'><ins id='FedGe'></ins><ul id='FedGe'></ul><sub id='FedGe'></sub></form><legend id='FedGe'></legend><bdo id='FedGe'><pre id='FedGe'><center id='FedGe'></center></pre></bdo></b><th id='FedGe'></th></span></q></dt></tr></i><div id='FedGe'><tfoot id='FedGe'></tfoot><dl id='FedGe'><fieldset id='FedGe'></fieldset></dl></div>
                  本文介绍了将 Windows 应用程序中的 console.writeline 重定向到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个用 C# 编写的外部 dll,我从程序集文档中研究了它使用 Console.WriteLine 将其调试消息写入控制台.

                  I have an external dll written in C# and I studied from the assemblies documentation that it writes its debug messages to the Console using Console.WriteLine.

                  这个 DLL 在我与应用程序的 UI 交互期间写入控制台,所以我不直接调用 DLL,但我会捕获所有控制台输出,所以我想我必须在表单加载中初始化,然后得到那个稍后捕获文本.

                  this DLL writes to console during my interaction with the UI of the Application, so i don't make DLL calls directly, but i would capture all console output , so i think i got to intialize in form load , then get that captured text later.

                  我想将所有输出重定向到一个字符串变量.

                  I would like to redirect all the output to a string variable.

                  我试过Console.SetOut,但它用来重定向到字符串并不容易.

                  I tried Console.SetOut, but its use to redirect to string is not easy.

                  推荐答案

                  您似乎想实时捕获控制台输出,我想您可以创建自己的 TextWriter 实现每当 Console 上发生 WriteWriteLine 时触发事件.

                  As it seems like you want to catch the Console output in realtime, I figured out that you might create your own TextWriter implementation that fires an event whenever a Write or WriteLine happens on the Console.

                  作者长这样:

                      public class ConsoleWriterEventArgs : EventArgs
                      {
                          public string Value { get; private set; }
                          public ConsoleWriterEventArgs(string value)
                          {
                              Value = value;
                          }
                      }
                  
                      public class ConsoleWriter : TextWriter
                      {
                          public override Encoding Encoding { get { return Encoding.UTF8; } }
                  
                          public override void Write(string value)
                          {
                              if (WriteEvent != null) WriteEvent(this, new ConsoleWriterEventArgs(value));
                              base.Write(value);
                          }
                  
                          public override void WriteLine(string value)
                          {
                              if (WriteLineEvent != null) WriteLineEvent(this, new ConsoleWriterEventArgs(value));
                              base.WriteLine(value);
                          }
                  
                          public event EventHandler<ConsoleWriterEventArgs> WriteEvent;
                          public event EventHandler<ConsoleWriterEventArgs> WriteLineEvent;
                      }
                  

                  如果它是 WinForm 应用程序,您可以像这样在 Program.cs 中设置编写器并使用其事件:

                  If it's a WinForm app, you can setup the writer and consume its events in the Program.cs like this:

                      /// <summary>
                      /// The main entry point for the application.
                      /// </summary>
                      [STAThread]
                      static void Main()
                      {
                          using (var consoleWriter = new ConsoleWriter())
                          {
                              consoleWriter.WriteEvent += consoleWriter_WriteEvent;
                              consoleWriter.WriteLineEvent += consoleWriter_WriteLineEvent;
                  
                              Console.SetOut(consoleWriter);
                              Application.EnableVisualStyles();
                              Application.SetCompatibleTextRenderingDefault(false);
                              Application.Run(new Form1());
                          }
                      }
                  
                      static void consoleWriter_WriteLineEvent(object sender, Program.ConsoleWriterEventArgs e)
                      {
                          MessageBox.Show(e.Value, "WriteLine");
                      }
                  
                      static void consoleWriter_WriteEvent(object sender, Program.ConsoleWriterEventArgs e)
                      {
                          MessageBox.Show(e.Value, "Write");
                      }
                  

                  这篇关于将 Windows 应用程序中的 console.writeline 重定向到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何中断 Console.ReadLine 下一篇:如何获取控制台应用程序窗口的句柄

                  相关文章

                  最新文章

                • <small id='Co6sQ'></small><noframes id='Co6sQ'>

                  <i id='Co6sQ'><tr id='Co6sQ'><dt id='Co6sQ'><q id='Co6sQ'><span id='Co6sQ'><b id='Co6sQ'><form id='Co6sQ'><ins id='Co6sQ'></ins><ul id='Co6sQ'></ul><sub id='Co6sQ'></sub></form><legend id='Co6sQ'></legend><bdo id='Co6sQ'><pre id='Co6sQ'><center id='Co6sQ'></center></pre></bdo></b><th id='Co6sQ'></th></span></q></dt></tr></i><div id='Co6sQ'><tfoot id='Co6sQ'></tfoot><dl id='Co6sQ'><fieldset id='Co6sQ'></fieldset></dl></div>

                    • <bdo id='Co6sQ'></bdo><ul id='Co6sQ'></ul>

                    1. <legend id='Co6sQ'><style id='Co6sQ'><dir id='Co6sQ'><q id='Co6sQ'></q></dir></style></legend>

                      <tfoot id='Co6sQ'></tfoot>