C# WPF程序增加终端串口打印调试信息
2021/9/9 17:04:18
本文主要是介绍C# WPF程序增加终端串口打印调试信息,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
打开 WPF工程该文件
增加 如下代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.Data; 5 using System.Diagnostics; 6 using System.IO; 7 using System.Linq; 8 using System.Runtime.InteropServices; 9 using System.Threading.Tasks; 10 using System.Windows;
1 public static class ConsoleManager 2 { 3 private const string Kernel32_DllName = "kernel32.dll"; 4 [DllImport(Kernel32_DllName)] 5 private static extern bool AllocConsole(); 6 [DllImport(Kernel32_DllName)] 7 private static extern bool FreeConsole(); 8 [DllImport(Kernel32_DllName)] 9 private static extern IntPtr GetConsoleWindow(); 10 [DllImport(Kernel32_DllName)] 11 private static extern int GetConsoleOutputCP(); 12 public static bool HasConsole 13 { 14 get { return GetConsoleWindow() != IntPtr.Zero; } 15 } 16 /// Creates a new console instance if the process is not attached to a console already. 17 public static void Show() 18 { 19 #if DEBUG 20 if (!HasConsole) 21 { 22 AllocConsole(); 23 InvalidateOutAndError(); 24 } 25 #endif 26 } 27 /// If the process has a console attached to it, it will be detached and no longer visible. Writing to the System.Console is still possible, but no output will be shown. 28 public static void Hide() 29 { 30 #if DEBUG 31 if (HasConsole) 32 { 33 SetOutAndErrorNull(); 34 FreeConsole(); 35 } 36 #endif 37 } 38 public static void Toggle() 39 { 40 if (HasConsole) 41 { 42 Hide(); 43 } 44 else 45 { 46 Show(); 47 } 48 } 49 static void InvalidateOutAndError() 50 { 51 Type type = typeof(System.Console); 52 System.Reflection.FieldInfo _out = type.GetField("_out", 53 System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); 54 System.Reflection.FieldInfo _error = type.GetField("_error", 55 System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); 56 System.Reflection.MethodInfo _InitializeStdOutError = type.GetMethod("InitializeStdOutError", 57 System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); 58 Debug.Assert(_out != null); 59 Debug.Assert(_error != null); 60 Debug.Assert(_InitializeStdOutError != null); 61 _out.SetValue(null, null); 62 _error.SetValue(null, null); 63 _InitializeStdOutError.Invoke(null, new object[] { true }); 64 } 65 static void SetOutAndErrorNull() 66 { 67 Console.SetOut(TextWriter.Null); 68 Console.SetError(TextWriter.Null); 69 } 70 }
修改增加运行语句:
1 /// <summary> 2 /// Interaction logic for App.xaml 3 /// </summary> 4 public partial class App : Application 5 { 6 App() 7 { 8 ConsoleManager.Show(); 9 } 10 }
OK
这篇关于C# WPF程序增加终端串口打印调试信息的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#