2

I cannot find out which font the console app uses by default? Is it guaranteed that everyone has that font (when running this .NET app)? Want to display some unicode chars and need to be sure they are present within that font. Thanks

Loj
  • 1,159
  • 3
  • 14
  • 23

2 Answers2

9

I strongly recommend avoiding the Console if you want to use Unicode characters. There are many issues with trying to get the Console to display Unicode correctly.

Unicode is not directly supported in Console output. The best option is typically to set the console's code page, which will require P/Invoke.

That being said, a GUI solves all of these issues, in a much nicer fashion. If you need Unicode output, I'd recommend a simple GUI.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    You can vote this down - but wait until you actually try to write unicode to the console - it's incredibly problematic... Even if you specify a TrueType font for the Console, the code page in effect tends to mess up unicode output. – Reed Copsey Oct 19 '10 at 19:07
  • @Dervin: The OP is trying to do something that isn't directly supported. – Reed Copsey Oct 19 '10 at 19:08
  • I do not think so as in C# every string is string of unicode characters. – Loj Oct 19 '10 at 19:08
  • 1
    @Mojmi: True. And every string in C# uses Unicode characters. However, the Console is using a native API (http://msdn.microsoft.com/en-us/library/ms682087(VS.85).aspx) that predates C#, and doesn't support unicode output directly. – Reed Copsey Oct 19 '10 at 19:09
  • @Mojmi: It depends on the character, and whether it's handled in the currently selected code page. There are only 256 characters at a time that "work just fine". Read the link I posted in my question, and it will explain the issue. – Reed Copsey Oct 19 '10 at 19:12
  • I upvoted because it's true, however there is also a unicode code page (65001). – Vilx- Oct 19 '10 at 19:14
  • @Vilx: There is - and it will work, mostly. You can make this work, but as I said, it's problematic at times... – Reed Copsey Oct 19 '10 at 19:15
  • @Reed Copsey: Well on the link I cannot see the issue exactly. But if I try specific character, can I be sure it will be displayed on different PCs as well if there has to be .NET installed? Or the code page can be different too – Loj Oct 19 '10 at 19:25
  • @Mojmi: The code page can differ on every system. It's typically based on the locale of the system. – Reed Copsey Oct 19 '10 at 19:26
  • @Reed Copsey: Thanks. Well, that is not good for me..But still there are lot of C# text games using unicode characters and working fine. Maybe they just rely on the the same culture of the system – Loj Oct 19 '10 at 19:28
  • @Mojmi: Yeah - it'll *probably* work everywhere. If you want to make sure that things always work, and need unicode, a GUI really is nicer... – Reed Copsey Oct 19 '10 at 19:35
  • The native-UTF-16-string `WriteConsoleW` interface does work for all characters and codepages. It's just the byte-string `WriteConsoleA` interface (and the MS C runtime's implementation of the stdio `printf`-etc functions that rely on `WriteConsoleA`) that end up in ‘ANSI’ with all its horrid system codepage problems. (And `chcp 65001` to get UTF-8 doesn't *quite* completely work, though it's good enough for many uses.) However, even if you can write Unicode there are still big problems with fonts; locales can use different default console fonts, so you can't really rely on anything. – bobince Oct 19 '10 at 23:17
  • You reasonably suggest not using the Console, but do not provide an alternative. "A GUI" is not an alternative, because Console I/O is much simpler and less cumbersome for the programmer. – Superbest Feb 12 '14 at 13:42
1

You can tell what font is being used by reading the registry value "0" from this key:

HKLM\Software\Microsoft\Windows NT\CurrentVersion\Console\TrueTypeFont
Dave Markle
  • 95,573
  • 20
  • 147
  • 170
  • 1
    That's only the default console font for locales that don't have exceptions. Some languages define other values. For example if you have a Windows install with the East Asian internationalisation files installed, you'll see the value `932`: `*MS ゴシック`, `*MS Gothic`. This means that when the system codepage is 932 (the Windows shift-JIS knock-off), as it is on a Japanese system, the font will be MS Gothic instead of the font listed in `0`. When you change the codepage with `chcp` you can even see it switch fonts. – bobince Oct 20 '10 at 01:34
  • In my experience, this registry key seems to affect the Windows console but not the .NET object `System.Console`. – H2ONaCl Jul 13 '14 at 15:08