visual-studio 如何更改 csharp 项目(visual studio / msbuild 机器)中的源文件编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/840065/
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 10:28:04  来源:igfitidea点击:

how to change source file encoding in csharp project (visual studio / msbuild machine)?

visual-studioencodingutf-8localization

提问by tomo

Is there a way to force VS to use Unicode always, instead of weird ISO-something?

有没有办法强制 VS 始终使用 Unicode,而不是奇怪的 ISO 东西?

I'm working on a winapp csproject using Visual Studio 2008 on Vista (Polish locale). The problem appears when I build the project on Win Server 2003 (English locale) - then Polish diacritic is gone.

我正在使用 Vista(波兰语语言环境)上的 Visual Studio 2008 开发一个 winapp csproject。当我在 Win Server 2003(英语语言环境)上构建项目时出现问题 - 然后波兰语变音符号消失了。

I investigated that the issue is caused by improper source file encoding. It looks that source files aren't using UTF-8 encoding.

我调查了这个问题是由不正确的源文件编码引起的。看起来源文件没有使用 UTF-8 编码。

回答by ruslander

Problem: Compiler launched from command line cannot process the source file because of international characters that I've just added.

问题:由于我刚刚添加的国际字符,从命令行启动的编译器无法处理源文件。

Solution: Save source file explicitly in UTF-8 encoding to preserve international characters.

解决方案:以 UTF-8 编码显式保存源文件以保留国际字符。

How To:

如何:

  • open the problematic file in Visual Studio.
  • on the File menu click “Advanced Save Options“
  • from “Encoding” combo select “Unicode (UTF-8 …“
  • click OK.
  • 在 Visual Studio 中打开有问题的文件。
  • 在文件菜单上单击“高级保存选项”
  • 从“编码”组合中选择“Unicode (UTF-8 ...”
  • 单击确定。

You're set. Commit to please the build server and rest of the team waiting for green.

你准备好了。承诺取悦构建服务器和等待绿色的团队其他成员。

回答by Ilya Chernomordik

In the latest Visual Studio 2017 the interface is changed a bit and you have to chose File -> Save [filename] As…. Then in the opened dialog you can chose Save with Encoding….

在最新的 Visual Studio 2017 中,界面发生了一些变化,您必须选择File -> Save [filename] As…. 然后在打开的对话框中您可以选择Save with Encoding…

enter image description here

在此处输入图片说明

回答by Ania

I've had the same problems with VS2015 vb.net project and solved it by adding <CodePage>1250</CodePage>marker to your vbproj/csproj file. I did it after <SchemaVersion>marker, rebuilt the project and it worked for me. I've had trouble with polish letters such as ?,? etc. You can also try the UTF-8 - 65001 but I didn't check if it works.

我在 VS2015 vb.net 项目中遇到了同样的问题,并通过<CodePage>1250</CodePage>在 vbproj/csproj 文件中添加标记来解决它。我在<SchemaVersion>标记之后做了它,重建了项目并且它对我有用。我在使用波兰语字母时遇到了麻烦,例如 ?,? 等等。你也可以试试 UTF-8 - 65001 但我没有检查它是否有效。