将C#控制台应用程序设置为Unicode输出 [英] Set C# console application to Unicode output

查看:187
本文介绍了将C#控制台应用程序设置为Unicode输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#控制台应用程序,并且我正在尝试在其中做一些ASCII艺术。但是,我想使用的某些字符是Unicode。因此,我在Internet / SO中进行搜索,找不到关于如何在C#控制台应用程序中将控制台设置为Unicode的统一答案。

I have a C# console application, and I was trying to do some ASCII art within it. However, some of the characters I wanted to use are Unicode. So, I was searching the internet/SO and couldn't find a consolidated answer on how to set the console to be Unicode in a C# console application.

TDLR:如何在C#控制台应用程序中将控制台设置为Unicode?

编辑:我确实找到了这篇文章

推荐答案

事实证明,需要进行多项设置才能使控制台显示Unicode字符。

It turns out that there are multiple things you need to set up in order to make the console display Unicode characters.


  1. 将控制台设置为支持Unicode的字体。为此,请使用 Console.ReadKey()运行一次C#控制台应用程序。 ,以便窗口保持打开状态。右键单击窗口的标题栏,然后选择属性。通过Visual Studio调试时,这些选项将保留。 您可能需要使用默认菜单,而不是将选项保留在整个系统中。字体选项卡中,您需要将字体设置为 Lucida。控制台。该字体支持Unicode字符。可以在此处找到相关帖子。

  2. 设置控制台的代码页转换为UTF-8。这有点棘手。因为,您必须在控制台窗口中执行命令以更改代码页。无论出于何种原因,此选项都不能用作控制台首选项。为此,您需要制作一个单独的 cmd.exe 进程,并使用它代替提供的常规控制台。

  1. Set the console to a Unicode supported font. To do this, run your C# console application once with Console.ReadKey(); so the window stays open. Right click on the title bar of the window and select Properties. These options will persist when debugging through Visual Studio. You might need to use the Default menu instead for persisting the options throughout the system. In the Fonts tab, you need to set the font to Lucida Console. This font supports Unicode characters. The related post can be found here.
  2. Set the console's code page to UTF-8. This one is a bit tricky. Because, you have to execute a command in the console window to change the code page. For whatever reason, this option is not available as a console preference. To do this, you'll need to make a separate cmd.exe process, and use this instead of the normal console provided.

var cmd = new Process
{
    StartInfo =
    {
        FileName = "cmd.exe",
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        CreateNoWindow = true,
        UseShellExecute = false
    }
};
cmd.Start();

cmd.StandardInput.WriteLine("chcp 65001");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();

上面代码的第一部分将创建一个新的 cmd.exe 进程。为 StartInfo 提供的设置将确保 Console 重定向到此新进程。代码的第二部分将命令发送到此控制台窗口并运行它。该命令 chcp 65001 将控制台的代码页设置为UTF-8。可以在此处这里

The first part of the code above will create a new cmd.exe process. The settings given to StartInfo will make sure that Console is redirected to this new process. The second part of the code sends a command to this console window and runs it. That command, chcp 65001, sets the console's code page to UTF-8. Related posts can be found here and here.

将OutputEncoding设置为UTF-8。这是 Console.WriteLine 实际上将输出Unicode字符。设置起来非常简单。

Set the OutputEncoding to UTF-8. This is the only way that Console.WriteLine will actually output Unicode characters. Setting this is very simple.

Console.OutputEncoding = Encoding.UTF8;

现在,控制台的任何输出将是在Unicode中。相关文章可以在此处找到。

Now, any output from Console will be in Unicode. The related post can be found here.

就这样!希望这些信息对某人有所帮助。 :-)

So, that's it! I hope this information helps someone. :-)

这篇关于将C#控制台应用程序设置为Unicode输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆