locked
C# Console application RRS feed

  • Question

  • I am creating console application for reading text file.But my text file contains japanese characters and I want to print that characters on console. I already tried using Console.OutputEncoding to Unicode But for that I need to change console font to "MS Gothic" manually. Is there any alternate way to print japanese characters to console application ?

    Thursday, August 6, 2020 4:48 PM

All replies

  • To use Unicode in the console window you must set the OutputEncoding to UTF8 (and/or UTF16 starting with 4.5). But you must also be using a TrueType font as documented here. MS Gothic is one such font but you can use any TTF. However not all fonts support all Unicode characters so you might have to link fonts as discussed in the above link. 

    The link to the specific documentation on how Console works with Unicode has the example code as well. The net summary is that you can use Unicode but there are no guarantees that all characters will print out irrelevant of your code. 


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, August 6, 2020 5:21 PM
  • There is a function that changes the font programmatically: SetCurrentConsoleFontEx [https://docs.microsoft.com/en-us/windows/console/setcurrentconsolefontex]. If it works, you can develop a Class Library in C++/CLR and call it from C#. Or try a complex invocation: https://social.msdn.microsoft.com/Forums/en-US/c276b9ae-dc4c-484a-9a59-1ee66cf0f1cc.  

    Thursday, August 6, 2020 6:01 PM
  • Hi BayMax5,

    Thank you for posting here.

    You can also try this code to modify the font at runtime.

        public static class ConsoleHelper
        {
            private const int FixedWidthTrueType = 54;
            private const int StandardOutputHandle = -11;
    
            [DllImport("kernel32.dll", SetLastError = true)]
            internal static extern IntPtr GetStdHandle(int nStdHandle);
    
            [return: MarshalAs(UnmanagedType.Bool)]
            [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            internal static extern bool SetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx);
    
            [return: MarshalAs(UnmanagedType.Bool)]
            [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            internal static extern bool GetCurrentConsoleFontEx(IntPtr hConsoleOutput, bool MaximumWindow, ref FontInfo ConsoleCurrentFontEx);
    
    
            private static readonly IntPtr ConsoleOutputHandle = GetStdHandle(StandardOutputHandle);
    
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
            public struct FontInfo
            {
                internal int cbSize;
                internal int FontIndex; 
                internal short FontWidth;
                public short FontSize;
                public int FontFamily;
                public int FontWeight;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
                //[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.wc, SizeConst = 32)]
                public string FontName;
            }
    
            public static FontInfo[] SetCurrentFont(string font, short fontSize = 0)
            {
                Console.WriteLine("Set Current Font: " + font);
    
                FontInfo before = new FontInfo
                {
                    cbSize = Marshal.SizeOf<FontInfo>()
                };
    
                if (GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref before))
                {
    
                    FontInfo set = new FontInfo
                    {
                        cbSize = Marshal.SizeOf<FontInfo>(),
                        FontIndex = 0,
                        FontFamily = FixedWidthTrueType,
                        FontName = font,
                        FontWeight = 400,
                        FontSize = fontSize > 0 ? fontSize : before.FontSize
                    };
    
                    // Get some settings from current font.
                    if (!SetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref set))
                    {
                        var ex = Marshal.GetLastWin32Error();
                        Console.WriteLine("Set error " + ex);
                        throw new System.ComponentModel.Win32Exception(ex);
                    }
    
                    FontInfo after = new FontInfo
                    {
                        cbSize = Marshal.SizeOf<FontInfo>()
                    };
                    GetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref after);
    
                    return new[] { before, set, after };
                }
                else
                {
                    var er = Marshal.GetLastWin32Error();
                    Console.WriteLine("Get error " + er);
                    throw new System.ComponentModel.Win32Exception(er);
                }
            }
        }
    ConsoleHelper.SetCurrentFont("KaiTi", 18);

    The code comes from this link.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 7, 2020 5:58 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 12, 2020 9:45 AM
  • Wednesday, August 12, 2020 4:25 PM