UPDATE : The ‘Macros’ is no longer found under the ‘Tools’ menu in VS2012. You can download this extension for VS 2012 that provides macros capability – though I found this powershell method to be more effective (handles entire folder of .cs files at one go).

For VS 2011 and earlier (this method only applicable to VS 2011 and earlier) – I found a simple macro to help with this task. From Visual Studio, navigate to Tools—>Macros—>New Macro Project (This launches a new IDE)  – and copy and paste the code below.  When you ‘run (Debug)’ this macro, it will insert the copyright notice into the currently open source file. You do have to run this for each source file. If you want a short(er) cut, simply define a context menu shortcut to this macro – as described in this post.

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module AddCopyrightHeader

    Sub FileHeader()

        Dim doc As Document
        Dim docName As String
        Dim companyName As String = "ANUJ Technologies Inc."
        Dim authorName As String = "Anuj Varma, www.anujvarma.com"
        Dim copyrightText As String = "Copyright (c) " + DateString.ToString() + " All Right Reserved"
        Dim summaryText As String = "Class representing a {0} entity."

        ' 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()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.LineUp()

        ' Write copyright tag
        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>"

        ' Write date tag (optional)
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// <date>" + DateString.ToString() + "</date>"

        ' Write summary tag (optional)
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// <summary>" + String.Format(summaryText, docName) + "</summary>"
        DTE.ActiveDocument.Selection.NewLine()

    End Sub

End Module

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.