Microsoft Visual Studio 2022调试控制台无法正确显示中文。

huangapple go评论119阅读模式
英文:

Microsoft Visual Studio 2022 debug Console cannot display correctly chinese

问题

我真的没有预料到 Visual Studio 2022 版本无法正确显示中文!我之前使用 JetBrains Rider。它内置了控制台。运行时,它总是能够正确显示中文,就像 VSCode 的内置终端一样。

我知道这一定是因为代码运行的字符编码与终端的设置不匹配。但这不应该由用户手动修改...

我的代码:

Console.WriteLine("Hello World!");
// 这里不起作用
// 输出: ??,??!
Console.WriteLine("你好,世界!");

输出:
Microsoft Visual Studio 2022调试控制台无法正确显示中文。

额外信息:

  • 我的操作系统:Windows 10
  • Windows 终端作为默认终端
  • Visual Studio 使用 "Windows 控制台主机" 作为默认控制台,似乎不支持 utf-8。(我已将 Windows 终端设置为默认终端)
  • 当我将默认终端设置为 "Windows 控制台主机" 时,请右键查看终端的字符编码:
    Microsoft Visual Studio 2022调试控制台无法正确显示中文。
    Microsoft Visual Studio 2022调试控制台无法正确显示中文。

也许将 Visual Studio 的默认控制台替换为 PowerShell 可以解决这个问题?


谢谢大家!

英文:

I really didn't expect that the visual studio 2022 version could not display Chinese correctly! I used JetBrains Rider before. It has a built-in console. When running, it will always display Chinese correctly, just like VSCode's built-in terminal.

I know it must be because the character code the code is running on doesn't match the terminal's settings. But this should not be manually modified by the user. . .

my code:

Console.WriteLine("Hello World!");
// here does not work
// output: ??,??!
Console.WriteLine("你好,世界!");

output:
Microsoft Visual Studio 2022调试控制台无法正确显示中文。

extra information:

  • my operating system: windows 10
  • windows terminal as defautl terminal
  • Visual Studio uses "windows console host" as the default console, it doesn't seem to support utf-8. (I have set windows terminal as default terminal)
  • When I set the default terminal to "Windows Console Host", right-click to view the character encoding of the terminal:
    Microsoft Visual Studio 2022调试控制台无法正确显示中文。
    Microsoft Visual Studio 2022调试控制台无法正确显示中文。

Maybe replacing the default console of visual studio with powershell can solve the problem?


thank you all

答案1

得分: 2

我找到了一个临时解决方案(需要添加一行代码):

在我的情况下(Windows 10),Visual Studio 使用一个名为“Windows Console Host”的默认终端,似乎不支持UTF-8,

因为当我添加了以下代码时:

Console.OutputEncoding = Encoding.UTF8;

中文输出从“问号”变成了“奇怪的符号”,所以我推断Visual Studio Debug Console使用的终端可能不支持UTF-8:

Microsoft Visual Studio 2022调试控制台无法正确显示中文。

所以我在“Windows终端”的设置中将默认终端更改为“Windows终端”:

Microsoft Visual Studio 2022调试控制台无法正确显示中文。

这次成功打印了中文(“Windows终端”必须支持UTF-8):

Microsoft Visual Studio 2022调试控制台无法正确显示中文。

英文:

I found a temporary solution (as one line of code needs to be added):

In my case (windows 10), Visual Studio uses a console called "Windwos Console Host" as the default terminal, which doesn't seem to support utf-8

because when I added the following code:

Console.OutputEncoding = Encoding.UTF8;

the Chinese output changed from "question mark" to "strange symbols", so I deduce that the terminal used by Visual Studio Debug Console probably does not support UTF-8:

Microsoft Visual Studio 2022调试控制台无法正确显示中文。

So I changed the default terminal to "windows terminal" in the settings of "windows terminal":

Microsoft Visual Studio 2022调试控制台无法正确显示中文。

This time successfully printed Chinese ("windows terminal" must support utf-8):

Microsoft Visual Studio 2022调试控制台无法正确显示中文。

huangapple
  • 本文由 发表于 2023年6月18日 19:18:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76500267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定