visual-studio Visual Studio 编码问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4132704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:43:25  来源:igfitidea点击:

Visual Studio encoding problems

visual-studiovisual-studio-2008

提问by jwaliszko

I have problems with files encoding in Visual Studio 2008. While compiling I'm getting such errors:

我在 Visual Studio 2008 中遇到文件编码问题。在编译时我遇到了这样的错误:

alt text

替代文字

When I'm trying to open file where particular error occures, encoding window appears:

当我尝试打开发生特定错误的文件时,会出现编码窗口:

alt text

替代文字

By defualt auto-detect is set. When I change encoding option to UTF-8, everything works. If I open each problematic file in my project using UTF-8 encoding, project starts to compile. The problem is I have too many files and there is ridiculous to open each file and set encoding to UTF-8. Is there any way to make this in a quick way ?

通过默认自动检测设置。当我将编码选项更改为 UTF-8 时,一切正常。如果我使用 UTF-8 编码打开项目中的每个有问题的文件,项目就会开始编译。问题是我有太多文件,打开每个文件并将编码设置为 UTF-8 是荒谬的。有没有办法快速做到这一点?

My VS settings are:

我的 VS 设置是:

alt text

替代文字

I'm using Windows Server 2008 R2.

我使用的是 Windows Server 2008 R2。

UPDATE:

更新:

For Hans Passant and Noah Richards. Thanks for interaction. I recently changed my operating system so everything is fresh. I've also downloaded fresh solution from source control.

对于汉斯·帕森特和诺亚·理查兹。谢谢互动。我最近改变了我的操作系统,所以一切都是新鲜的。我还从源代码管理下载了新的解决方案。

In OS regional settings I've changed system locale to Polish(Poland):

在操作系统区域设置中,我已将系统区域设置更改为波兰语(波兰):

alt text

替代文字

In VS I've changed international settings to the same as windows:

在 VS 中,我已将国际设置更改为与 Windows 相同:

alt text

替代文字

The problem is still not solved.

问题仍然没有解决。

When I open some .cs files using auto-detection for encoding, and then check Files -> Advanced Save Options..., some of this .cs files have codepage 1250:

当我使用自动检测编码打开一些 .cs 文件,然后检查Files -> Advanced Save Options... 时,其中一些 .cs 文件的代码页为 1250:

alt text

替代文字

but internally looks following:

但内部看起来如下:

alt text

替代文字

It is wired, because when I check properties of such particular files in source control, they seems to have UTF-8 encoding set:

它是有线的,因为当我在源代码管理中检查此类特定文件的属性时,它们似乎设置了 UTF-8 编码:

alt text

替代文字

I don't understand this mismatch.

我不明白这种不匹配。

All other files have UTF-8 encoding:

所有其他文件都有 UTF-8 编码:

alt text

替代文字

and opens correctly. I have basically no idea what is going wrong because as far as I know my friend has the same options set as me, and the same project compiles for him correctly. But so far he happily hasn't encountered encoding issues.

并正确打开。我基本上不知道出了什么问题,因为据我所知,我的朋友设置了与我相同的选项,并且同一个项目为他正确编译。但到目前为止,他很高兴还没有遇到编码问题。

采纳答案by Mihai Nita

That uppercase A with circumflex tells me that the file is UTF-8 (if you look with a hex editor you will probably see that the bytes are C2 A0). That is a non-breaking space in UTF-8.

带抑扬符的大写 A 告诉我该文件是 UTF-8(如果您使用十六进制编辑器查看,您可能会看到字节是 C2 A0)。这是 UTF-8 中的不间断空格。

Visual Studio does not detect the encoding because (most likely) there are not enough high-ASCII characters in the file to help with a reliable detection.

Visual Studio 不检测编码,因为(很可能)文件中没有足够的高位 ASCII 字符来帮助进行可靠的检测。

Also, there is no BOM (Byte Order Mark). That would help with the detection (this is the "signature" in the "UTF-8 with signature" description).

此外,没有 BOM(字节顺序标记)。这将有助于检测(这是“带有签名的 UTF-8”描述中的“签名”)。

What you can do: add BOM to all the files that don't have one. How to add? Make a file with a BOM only (empty file in Notepad, Save As, select UTF-8 as encoding). It will be 3 bytes long (EF BB BF). You can copy that at the beginning of each file that is missing the BOM:

您可以做什么:将 BOM 添加到所有没有 BOM 的文件中。如何添加?制作一个只有 BOM 的文件(记事本中的空文件,另存为,选择 UTF-8 作为编码)。它将是 3 个字节长(EF BB BF)。您可以在每个缺少 BOM 的文件的开头复制它:

   copy /b/v BOM.txt + YourFile.cs YourFile_Ok.cs
   ren YourFile.cs YourFile_Org.cs
   ren YourFile_Ok.cs YourFile.cs

Make sure there is a + between the name of the BOM file and the one of the original file.

确保 BOM 文件的名称和原始文件的名称之间有一个 +。

Try it on one or two files, and if it works you can create some batch file to do that. Or a small C# application (since you are a C# programmer), that can detect if the file already has a BOM or not, so that you don't add it twice. Of course, you can do this in almost anything, from Perl to PowerShell to C++ :-)

在一个或两个文件上尝试一下,如果有效,您可以创建一些批处理文件来执行此操作。或者一个小的 C# 应用程序(因为你是一个 C# 程序员),它可以检测文件是否已经有一个 BOM,这样你就不会添加两次。当然,你几乎可以在任何东西中做到这一点,从 Perl 到 PowerShell 再到 C++ :-)

回答by Noah Richards

Once you've opened the files in UTF-8 mode, can you try changing the Advanced Save Options for the file and saving it (as UTF-8 with signature, if you think these files should be UTF-8)?

以 UTF-8 模式打开文件后,是否可以尝试更改文件的高级保存选项并将其保存(如果您认为这些文件应该是 UTF-8,则为带有签名的 UTF-8)?

The auto-detect encoding detection is best-effort, so it's likely that something in the file is causing it to be detected as something other than UTF-8, such as having only ASCII characters in the first kilobyte of the file, or having a BOM that indicates the file is something other than UTF-8. Re-saving the file as UTF-8 with signature should (hopefully) correct that.

自动检测编码检测是尽力而为,因此很可能是文件中的某些内容导致它被检测为 UTF-8 以外的其他内容,例如文件的第一个 KB 中只有 ASCII 字符,或者具有指示文件不是 UTF-8 的 BOM。将文件重新保存为带有签名的 UTF-8 应该(希望)更正。

If it continues happening after that, let me know, and we can try to track down what is causing them to be created/saved like that in the first place.

如果此后它继续发生,请告诉我,我们可以尝试追踪导致它们像这样创建/保存的原因。