在Winform程序中有时候调试会通过Console.Write()方式输出一些信息,这些信息是在Visual Studio的输出窗口显示。
所以就会想,能不能调用系统的Cmd窗口输出呢,经过一番查阅,发现是可以的,现在就把方法写下了:
主要用到的是win32 API函数实现的:
[DllImport("kernel32.dll")] static extern bool FreeConsole(); [DllImport("kernel32.dll")] public static extern bool AllocConsole();
在Program.cs文件中调用方法即可
完整代码:
using System; using System.Collections.Generic; using System.Windows.Forms; using System.Runtime.InteropServices; namespace XY.WinformDebug { static class Program { [DllImport("kernel32.dll")] static extern bool FreeConsole(); [DllImport("kernel32.dll")] public static extern bool AllocConsole(); /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { AllocConsole();//调用系统API,调用控制台窗口 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FrmMain()); FreeConsole();//释放控制台 } } }
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论