1、說明
由於控制檯api被封裝在kernel32.dll鏈接庫中,而kernel32.dll並未託管dll,所以需要使用DllImport來導入。
3、代碼
static class Program
{
/// <summary>
/// 啓動控制檯
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
/// <summary>
/// 釋放控制檯
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
#if DEBUG
AllocConsole();
// Console.WriteLine("aa");
#endif
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
#if DEBUG
FreeConsole();
#endif
}
}