在Visual Studio中给源代码添加文件头注释

这里我向大家介绍一个Visual studio的宏(Macro)来给源代码添加注释。

打开Visual studio->Tools->Macros->Marcros IDE,添加下面的代码:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics
 
Public Module MyVisualStudioModule
    Sub DocumentFileHeader()
        Dim doc As Document
        Dim docName As String
        Dim companyName As String = "YOUR COMPANY NAME"
        Dim authorName As String = "YOUR NAME"
        Dim copyrightText As String = String.Format("Copyright (c) {0} {1}. All rights reserved.", Date.Now.Year, companyName)
 
        ' Get the name of this object from the file name
        doc = DTE.ActiveDocument
 
        ' Get the name of the current document
        docName = doc.Name
 
        ' Set selection to top of document
        DTE.ActiveDocument.Selection.StartOfDocument()
 
        ' Write copyright tag
        DTE.ActiveDocument.Selection.Text = "//------------------------------------------------------------------------------"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// <copyright file=""" + docName + """ company=""" + companyName + """>"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "//     " + copyrightText
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// </copyright>"
 
        ' Write author name tag (optional)
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// <author>" + authorName + "</author>"
        DTE.ActiveDocument.Selection.NewLine()
 
        DTE.ActiveDocument.Selection.Text = "// <date>" + String.Format("{0:D}", Date.Now) + "</date>"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// <description>"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// </description>"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "//------------------------------------------------------------------------------"
        DTE.ActiveDocument.Selection.NewLine()
    End Sub
End Module

这样,当你每次运行这个宏的时候,都会给你Visual studio的当前的文档添加如下格式的文件头注释:

//------------------------------------------------------------------------------
// <copyright file="YOUR FILE NAME.cs" company="YOUR COMPANY NAME">
//     Copyright (c) 2009 YOUR COMPANY NAME. All rights reserved.
// </copyright>
// <author>YOUR NAME</author>
// <date>Monday, March 02, 2009</date>
// <description>
// </description>
//------------------------------------------------------------------------------

看起来还挺COOL的,哈。

posted @ 2010-11-15 12:50  夜深沉  阅读(1157)  评论(0编辑  收藏  举报