『QQ:1353814576』

.NET 5 WPF 如何开启控制台界面用于实时输出日志


.NET 5 WPF 如何开启控制台界面用于实时输出日志

这两天升级博客程序之余也做了些附加程序 其中一个是WPF集成cefsharp浏览器控件(.NET 5 更新支持了WPF 这就很棒) 做了个网页自动化定时任务程序,由于界面空间全给了浏览器控件导致没有足够的位置来显示日志给我自己看;

想着以前做.net framework开发时 wpf 是可以直接开启控制台模式的 只需要把属性里的输出类型改为控制台 启动就可以兼顾界面和控制台,界面看效果 控制台看日志 挺方便;如下图

.NET 5 WPF 如何开启控制台界面用于实时输出日志

但.NET 5里面设置后是无效的 还需要另外调用WinAPI 才能实现 效果

以下是实现代码:


    public static class ConsoleManager
    {
        private const string Kernel32_DllName = "kernel32.dll";
        [DllImport(Kernel32_DllName)]
        private static extern bool AllocConsole();
        [DllImport(Kernel32_DllName)]
        private static extern bool FreeConsole();
        [DllImport(Kernel32_DllName)]
        private static extern IntPtr GetConsoleWindow();
        [DllImport(Kernel32_DllName)]
        private static extern int GetConsoleOutputCP();
        public static bool HasConsole
        {
            get { return GetConsoleWindow() != IntPtr.Zero; }
        }
            get { return GetConsoleWindow() != IntPtr.Zero; }
        }
        /// Creates a new console instance if the process is not attached to a console already.  
        public static void Show()
        {

            if (!HasConsole)
            {
                AllocConsole();
                InvalidateOutAndError();
            }

#if DEBUG
            if (!HasConsole)
            {
                AllocConsole();
                InvalidateOutAndError();
            }
#endif
        }
        /// 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.   
        public static void Hide()
        {
#if DEBUG
            if (HasConsole)
            {
                SetOutAndErrorNull();
                FreeConsole();
            }
#endif
        }
        public static void Toggle()
        {
            if (HasConsole)
            {
                Hide();
            }
            else
            {
                Show();
            }
        }
        static void InvalidateOutAndError()
        {
            Type type = typeof(System.Console);
            System.Reflection.FieldInfo _out = type.GetField("_out",
                System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
            System.Reflection.FieldInfo _error = type.GetField("_error",
                System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
            System.Reflection.MethodInfo _InitializeStdOutError = type.GetMethod("InitializeStdOutError",
                System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
            //Debug.Assert(_out != null);
            //Debug.Assert(_error != null);
            //Debug.Assert(_InitializeStdOutError != null);
            //_out.SetValue(null, null);
            //_error.SetValue(null, null);
            //_InitializeStdOutError.Invoke(null, new object[] { true });
        }
        static void SetOutAndErrorNull()
        {
            Console.SetOut(TextWriter.Null);
            Console.SetError(TextWriter.Null);
        }
    }

调用方式

 ConsoleManager.Show();//开启控制台

程序启动的时候调用一次即可