Created
August 19, 2014 11:08
-
-
Save AuthorProxy/19afe13634f2145670f7 to your computer and use it in GitHub Desktop.
Prepare console for use with windows forms application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Runtime.InteropServices; | |
| using System.Text; | |
| using Microsoft.Win32.SafeHandles; | |
| namespace ConsoleUI | |
| { | |
| /// <summary> | |
| /// Configure console to use with WF | |
| /// </summary> | |
| internal class PrepareConsole | |
| { | |
| private const int STD_OUTPUT_HANDLE = -11; | |
| private const int MY_CODE_PAGE = 437; | |
| [DllImport("kernel32.dll" , EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet .Auto, CallingConvention = CallingConvention.StdCall)] | |
| private static extern IntPtr GetStdHandle(int nStdHandle); | |
| [DllImport("kernel32.dll" , EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet .Auto, CallingConvention = CallingConvention.StdCall)] | |
| private static extern int AllocConsole(); | |
| internal static void Configure() | |
| { | |
| AllocConsole(); | |
| IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE); | |
| SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true); | |
| FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write); | |
| Encoding encoding = Encoding .GetEncoding(MY_CODE_PAGE); | |
| StreamWriter standardOutput = new StreamWriter(fileStream, encoding); | |
| standardOutput.AutoFlush = true; | |
| Console.SetOut(standardOutput); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment