Use a Visual Studio macro to automatically add a file header to a code file

Source: Internet
Author: User

Generally, code files must have a document header with copyright instructions and help information. I used ctrlc and ctrlv in the past. Later I studied the online materials and found it convenient to use macros to complete this task.

Choose macro> macro IDE from the tool menu, add a new item in the macro editor, and select a module. Well, the module name is "addmediaentheader.

Edit the Code as follows:

Imports system
Imports envdte
Imports envdte80
Imports envdte90
Imports envdte90a
Imports envdte100
Imports system. Diagnostics

Public module addmediaentheader
Sub addmediaentheader ()
Dim document as document
Document = DTE. activedocument
Document. selection. startofdocument ()
Document. selection. gotoline (2, true)
Dim content as string = Document. selection. Text
'Compare the copyright information string of the second line of the Code to determine whether to add a file header.
Dim copyrightinformation as string = "Copyright (c) xwingyz (AT) gmail.com. All right reserved ."
Dim MS = "*" + copyrightinformation
Dim found = string. Compare (content, MS)
If (found <> 0) then
Document. selection. startofdocument ()
Document. selection. Lineup ()
Document. selection. Text = "/*-----------------------------------------------------------------------------"
Document. selection. newline ()
Document. selection. Text = copyrightinformation
Document. selection. newline ()
Document. selection. newline ()
Document. selection. newline ()
Document. selection. Text = "$ lastchangedby $"
Document. selection. newline ()
Document. selection. Text = "$ lastchangeddate $"
Document. selection. newline ()
Document. selection. Text = "$ lastchangedrevision $"
Document. selection. newline ()
Document. selection. Text = "-----------------------------------------------------------------------------"
Document. selection. newline ()
Document. selection. Text = "/"
Document. selection. newline ()
End if
End sub
End Module

Save and exit.

Open a code file, select macro> macro Resource Manager from the tool menu, right-click the created module addmediaentheader, and choose run from the shortcut menu to see if the file header is automatically added to the Code? Run the following command again to check if there is no repeated addition? Everything is okay without an accident.

 

The next step is to assign a shortcut key to this macro, which makes it easier.

Find the options in the tool menu-> keyboard options, enter addmediaentheader in the filter box, and you will see the macro you just created? Allocate a shortcut that you like.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.