Use Visual Studio 2019 to batch add code file headers

Application scenario introduction

When we use some open source projects, we basically see a copyright notice at the head of each source code file. The number of source code files in a project or solution can range from tens to thousands or even more, so how can we easily add or modify consistent file headers for so many files in batches? In the Visual Studio 2019 v16.7.1 version updated on August 11, 2020, the ability to use EditorConfig to add headers to existing files, projects, and solutions was added . Mention EditorConfig , then:

What is EditorConfig?

EditorConfigIt is a text file that helps multiple developers working on the same project across multiple editors and IDEs to maintain a consistent coding style. The EditorConfig file can be set such as indentation style, tab width, end-of-line characters and encoding, etc., regardless of the editor or IDE used. Adding an EditorConfig file to the project can force the implementation of a consistent coding style for all people who use the project. EditorConfig settings take precedence over the global Visual Studio text editor settings.

Since these settings are contained in the basic code file, they can be moved together with the basic code. As long as you open the code file in an EditorConfig compatible editor, you can implement the text editor settings. For more information about EditorConfig files, please refer to the EditorConfig.org website.

Use Visual Studio 2019 to add file headers

First, open in Visual Studio project or solution, click on "Help"> "About" to see your version of Visual Studio 2019 is whether 16.7.1or later, if not please upgrade.

  1. Add EditorConfig to the project or solution.

    According to the application .editorconfigobject settings (all items in the solution is one of the items), select the project or the solution node. You can also select a folder to add the project or solution .editorconfigfile.

    From the menu bar, select "Project"> "Add New Item", or press Ctrl+Shift+A:
    vs_file_header_template_0

    Or right-click the project, solution, or folder in the "Solution Explorer" and select "Add"> "New EditorConfig":
    vs_file_header_template_1

    After the addition is complete you can see the .editorconfigcontents of the file as follows:
    vs_file_header_template_2

  2. In .editorconfigadd file file_header_templateentries

    file_header_template = 添加文件头(add file header)示例程序\n Copyright (c) https://ittranslator.cn/
    

    .editorconfigIn line breaks, you need to use the Unix line break ( \n) to insert a new line.

    As shown in the figure:
    vs_file_header_template_3

  3. Place the cursor on the first line of any C # or Visual Basic file, press the Ctrl+.trigger "Access and reconstruction" menu, select "Add header" as:
    vs_file_header_template_4

    At "Repair all instances in the following objects:" you can select "Document", "Project" or "Solution". The following figure shows the preview effect after selecting "Project":
    vs_file_header_template_5

  4. Click the "Apply" button to apply the changes.

    At this point you can see all the code files in the project have added a .editorconfigset of file_header_templatestring entry as a file header:vs_file_header_template_6

Modify the file header

The steps for modifying the file header and adding the file header are the same.

  1. In the .editorconfigmodified file in file_header_templatevalue of the item

    Try adding a few words of "Technical Translation Station":

    file_header_template = 添加文件头(add file header)示例程序\n Copyright (c) 技术译站 https://ittranslator.cn/ 
    
  2. Repeat step 3 of "Add File Header" above, and the preview is as follows:
    vs_file_header_template_7

  3. Repeat step 4 of "Add File Header" above and click the "Apply" button, the result is as follows:
    vs_file_header_template_8

references


Author: Technical Zemin
Publisher: Technical Translation Station

Public Number: Technical Translation Station

Guess you like

Origin blog.csdn.net/weixin_47498376/article/details/108415208