ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# WPF程序增加终端串口打印调试信息

2021-09-09 16:03:56  阅读:216  来源: 互联网

标签:Reflection C# 串口 System static using 调试信息 null public


打开 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

标签:Reflection,C#,串口,System,static,using,调试信息,null,public
来源: https://www.cnblogs.com/chenxiaolinembed/p/15247355.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有