visual-studio 如何在 Visual Studio 中设置标准编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/696627/
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
How to set standard encoding in Visual Studio
提问by Thomaschaaf
I am searching for a way to setup Visual Studio so it always saves my files in UTF-8.
我正在寻找一种设置 Visual Studio 的方法,以便它始终以 UTF-8 格式保存我的文件。
I have only found options to set this project wide. Is there a way to set it Visual Studio wide?
我只找到了设置这个项目范围的选项。有没有办法在 Visual Studio 范围内设置它?
回答by Samuel Meacham
Do you want the files to save as UTF-8 because you are using special characters that would be lost in ASCII encoding?  If that's the case, then there is a VS2008 global setting in Tools > Options > Environment > Documents, named Save documents as Unicode when data cannot be saved in codepage.  When this is enabled, VS2008 will save as Unicode if certain characters cannot be represented in the otherwise-default codepage.
您是否希望将文件保存为 UTF-8,因为您正在使用在 ASCII 编码中会丢失的特殊字符?如果是这种情况,那么 中有一个 VS2008 全局设置Tools > Options > Environment > Documents,名为Save documents as Unicode when data cannot be saved in codepage. 启用此选项后,如果某些字符无法在其他默认代码页中表示,VS2008 将另存为 Unicode。
Also, which files are notbeing saved as UTF-8? All of my .cs, .csproj, .sln, .config, .as*x, etc, all save as UTF-8 (with signature, the byte order marks), by default.
另外,哪些文件没有保存为 UTF-8?默认情况下,我所有的 .cs、.csproj、.sln、.config、.as*x 等文件都保存为 UTF-8(带有签名、字节顺序标记)。
回答by Imre Pühvel
What
什么
It is possible with EditorConfig.
EditorConfig是可能的。
EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs.
EditorConfig 帮助开发者在不同的编辑器和 IDE 之间定义和维护一致的编码风格。
This also includes file encoding.
这也包括文件编码。
EditorConfig is built-in Visual Studio 2017 by default, and I there were plugins available for versions as old as VS2012. Read more from EditorConfig Visual Studio Plugin page.
EditorConfig 是默认内置的 Visual Studio 2017,我有一些插件可用于与 VS2012 一样旧的版本。从EditorConfig Visual Studio 插件页面阅读更多信息。
How
如何
You can set up a EditorConfig configuration file high enough in your folder structure to span all your intended repos  (up to your drive root should your files be really scattered everywhere) and configure the setting charset:
您可以在文件夹结构中设置一个足够高的 EditorConfig 配置文件以跨越所有预期的存储库(如果您的文件真的分散在任何地方,则直到您的驱动器根目录)并配置设置charset:
charset: set to latin1, utf-8, utf-8-bom, utf-16be or utf-16le to control the character set.
charset: 设置为 latin1, utf-8, utf-8-bom, utf-16be 或 utf-16le 来控制字符集。
You can add filters and exceptions etc on every folder level or by file name/type should you wish for finer control.
如果您希望更好地控制,您可以在每个文件夹级别或按文件名/类型添加过滤器和例外等。
Once configured then compatible IDEs should automatically do it's thing to make matching files comform to set rules. Note that Visual Studio does not automatically convert all your files but do its bit when you work with files in IDE (open and save).
配置完成后,兼容的 IDE 应自动执行使匹配文件符合设置规则的工作。请注意,Visual Studio 不会自动转换您的所有文件,但会在您在 IDE 中处理文件(打开和保存)时进行转换。
What next
接下来是什么
While you could have a Visual-studio-wide setup, I strongly suggest to still include an EditorConfig root to your solution version control, so that explicit settings are automatically synced to all team members as well. Your drive root editorconfig file can be the fallback should some project not have their own editorconfig files set up yet.
虽然您可以拥有 Visual-studio-wide 设置,但我强烈建议您仍将 EditorConfig 根包含到您的解决方案版本控制中,以便显式设置也会自动同步到所有团队成员。如果某些项目尚未设置自己的 editorconfig 文件,则您的驱动器根 editorconfig 文件可以作为后备。
回答by user10959888
I work with Windows7.
我使用 Windows7。
Control Panel - Region and Language - Administrative - Language for non-Unicode programs.
控制面板 - 区域和语言 - 管理 - 非 Unicode 程序的语言。
After I set "Change system locale" to English(United States). My default encoding of vs2010 change to Windows-1252. It was gb2312before.
在我将“更改系统区域设置”设置为英语(美国)之后。我的 vs2010 的默认编码更改为Windows-1252. 这是gb2312以前。
I created a new .cppfile for a C++ project, after checking in the new file to TFS the encoding show Windows-1252 from the properties page of the file.
我.cpp为 C++ 项目创建了一个新文件,在将新文件签入到 TFS 后,编码从文件的属性页面显示 Windows-1252。
回答by Shay Erlichmen
I don't know of a global setting nither but you can try this:
我也不知道全局设置,但你可以试试这个:
- Save all Visual Studio templates in UTF-8
- Write a Visual Studio Macro/Addin that will listen to the DocumentSaved event and will save the file in UTF-8 format (if not already).
- Put a proxy on your source control that will make sure that new files are always UTF-8.
- 以 UTF-8 格式保存所有 Visual Studio 模板
- 编写一个 Visual Studio 宏/插件,它将侦听 DocumentSaved 事件并将文件保存为 UTF-8 格式(如果还没有)。
- 在源代码控制上放置一个代理,以确保新文件始终为 UTF-8。

