C#在winform中调用系统控制台输出
生活随笔
收集整理的這篇文章主要介紹了
C#在winform中调用系统控制台输出
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?SeayXu 原文C#在winform中調用系統控制臺輸出
在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();//釋放控制臺 }} }?
轉載于:https://www.cnblogs.com/arxive/p/6043197.html
總結
以上是生活随笔為你收集整理的C#在winform中调用系统控制台输出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【drp 12】再识转发和重定向:Spr
- 下一篇: .NET、C#和ASP.NET,ASP.