What is the default encoding for source files in visual studio 2017

前端 未结 5 944
独厮守ぢ
独厮守ぢ 2020-12-16 13:13

It seems visual studio 2017 always saves new files as UTF8-BOM. It also seems this was not the case with earlier versions of visual studio, but i could not find any documen

相关标签:
5条回答
  • 2020-12-16 13:21

    Check Fix File Encoding extension that prevents Visual Studio 2017/2015/2013/2012 from adding BOM to UTF-8 files.

    Normally, when you edit a UTF-8 file in Visual Studio, it adds the byte order mark (BOM) sequence 0xEF, 0xBB, 0xBF to the beginning of the file. Sometimes it confuses other applications further processing the file. You can select an encoding manually (File - Advanced Save Options... or File > Save As... > Save with Encoding...), but you need to do it each time you reopen the file

    Also, this extension will answer most of your questions.

    Fix File Encoding automatically detects when a UTF-8 file is opened in Visual Studio and sets its encoding to UTF-8 without signature. If you don't edit the file, it remains unmodified. If you edit the file, it will be saved without the BOM.

    Fix File Encoding lets you configure which files to encode based on the file path and the file extension. By default, only .htm and .html files are protected from Visual Studio.

    0 讨论(0)
  • 2020-12-16 13:23

    Apparently the "Advanced Save Options\Encoding" option has been removed from the "File" menu due to uncommon use. This was the reason given by a Visual Studio Team member (see this).

    The option is still there, but you have to do a couple extra clicks.

    1. In the menu strip, Go To FILE -> Save as
    2. When the Save File Dialog appears, the Save button has a down arrow. Click it.
    3. Select Save with Encoding...

    Once you save a file to a certain format (I believe the one you're looking for is Unicode (UTF-8 without signature) - Codepage 65001), Visual Studio should in theory not change it on a whim.

    Now here's the problem though, once you remove the BOM, no reader can really know with 100% certainty that a given text file is actually UTF-8. This is just from observing the behaviour, but if you go and Save as With Encoding... and select
    Unicode (UTF-8 without signature) - Codepage 65001 (which is UTF8 without BOM), the BOM will be removed.....however, when you close the file and reopen it, then go to Advanced Save Options again, you will notice that Visual Studio assumed that the text format was CodePage 1252. The file will of course be perfectly valid as it maps every possible byte value to some character, but that may give you strange results in some fringe cases.

    One thing it will not do, is add the BOM back in (at least I have never seen it). Hope this helps.

    0 讨论(0)
  • 2020-12-16 13:30

    Also there has been an option "Advanced Save Options\Encoding" which did allow to change the encoding of newly saved files which is missing in VS2017.

    This feature Already exists! You can save files with specific character encoding to support bi-directional languages. You can also specify an encoding when opening a file, so that Visual Studio displays the file correctly.

    To save a file with encoding

    1. From the File menu, choose Save File As, and then click the drop-down button next to the Save button. The Advanced Save Options dialog box is displayed.
    2. Under Encoding, select the encoding to use for the file.
    3. Optionally, under Line endings, select the format for end-of-line characters.

    Are all files types saved with UTF8-BOM encoding in VS2017

    In my case, VS stores all the files with CodePage 1252 encoding.

    Is it possible to configure the encoding for new files in VS2017

    However, My Visual Studio version is 15.6.1 and some people have the same problem like yours in previous versions of 2017, but they said "We have fixed this issue and it's available in Visual Studio 2017 15.3"

    If not working, for C++ projects Take a look at /utf-8 (Set Source and Executable character sets to UTF-8).

    Will VS2017 change the encoding of "old" files which don't have UTF8-BOM

    By default, Visual Studio detects a byte-order mark to determine if the source file is in an encoded Unicode format, for example, UTF-16 or UTF-8. If no byte-order mark is found, it assumes the source file is encoded using the current user code page, unless you have specified a code page by using /utf-8 or the /source-charset option. Some people encountered a problem which is came from .editorconfig file, as below:

    root = true
    
    [*]
    indent_style = tab
    indent_size = 4
    tab_width = 4
    trim_trailing_whitespace = true
    insert_final_newline = true
    charset = utf-8 
    

    That final charset line is probable doing it... but I'm not asking for 'utf-8-with-bom'!

    0 讨论(0)
  • 2020-12-16 13:41

    Unfortunately this is to long as comment to Nik's answer, therefore I use another one:

    • VS saves all source code files (.cpp, .cs, .h, etc) and Web files (.htm(l), .css, .xml) in UTF-8 with BOM (with signature in MS jargon).

    • However, VS saves text files created by VS in the code page of the local settings, for example code page 1252 for Western European cultures. VS is clever enough to detect characters that can’t be encoded in the default code page and will prompt you to encode in UTF-8. Visual Studio will automatically save in UTF-8, with BOM of course, if you check "Save documents as Unicode when data cannot be saved in codepage" in the dialog box "Tools/Options/Environment/Documents".

    • You can override the encoding for each single file by using "Save As", but you can't override the default encodings in VS Options

    • But, you can override the default settings with an EditorConfig file. How: https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2019.

    0 讨论(0)
  • 2020-12-16 13:46

    You can use EditorConfig with the charset property to define encoding for source files in VS 2017.

    0 讨论(0)
提交回复
热议问题