Change encoding on a per file or per extension basis

。_饼干妹妹 提交于 2019-11-30 07:54:17

问题


I'm using Microsoft Visual Studio Express 2012 for Web. It seems that every file which I open with it gets encoded into UTF-8. For most files which are going to be web-facing, that's fine. However, I have files in my projects that are specifically for build purposes (e.g., .bat files), which must be encoded in ANSI.

Are there any configuration settings in VS to either designate on a per file or a per extension basis the encoding? Or, if not specify the encoding, at least disable the auto-conversion to UTF-8?


回答1:


Open the problematic file in Visual Studio and...

  • On the File menu, click Advanced Save Options.
  • In the Encoding dropdown, select Unicode (UTF-8 … or the encoding you require.
  • Click OK.

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




回答2:


An option to handle the encoding of all files of a given extension on a per open basis can be configured in the Options dialog. See MSDN page on Options, Text Editor, File Extension.

Navigate to Tools > Options > Text Editor > File Extension.

For the bat extension, I selected Source Code (Text) Editor with Encoding. The with Encoding part means that the user will be given options as to what encoding to use when opening the file. The default in this mode is Auto-detect, which preserves the ANSI encoding, if that is what the file already uses. Otherwise, one can explicitly designate it for the individual file.

Unfortunately, it doesn't seem to remember the setting last used when opening a file, and will thus prompt for an encoding setting every time a file is opened.




回答3:


I had code conversion problems width VS studio 2012 as well. Namely, I had non ansi compliant characters in strings ind my .js files and unreadable was outputted to the browsers html page. I figured out that accept script files (like .js) VS 2012 creates all files in UTF-8. *The problem is with the suggestion bellow to change the defaults in the options dialog resulted in that the syntax highlighting and intelisense stopped working in all .js files.* So my workaround solution know is that I convert my .js files with notepad++ to utf-8 without BOM. In this way my "unusual" chars are appearing well in browsers and the intelisense is working fine as well.



来源:https://stackoverflow.com/questions/12276757/change-encoding-on-a-per-file-or-per-extension-basis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!