今天在 github 网站中观察文件修改情况的时候,发现一个 .cs 文件中的汉字部分是乱码。意识到这是因为该文件的 Encoding 不是 UTF-8 造成的。
这篇文字介绍了如何修改一个源代码文件的 Encoding:
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.
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.
You’re set. Commit to please the build server and rest of the team waiting for green.
另一篇也有些参考价值:
http://stackoverflow.com/questions/696627/how-to-set-standard-encoding-in-visual-studio
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.
Also, which files are not being 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.