However, when saving, you will have to actually create a add-in for
VS.NET which will update the information for you, so it's better to just
look into creating an add-in and doing all the work there. It's going to be
more work than just a macro, mind you.
You can basically do this with a macro. Open the macro explorer and
navigate the file EnvironmentEvents. Add the following event handler at
the bottom of the file:
Private Sub DocumentEvents_DocumentOpened(ByVal document As
EnvDTE.Document) Handles DocumentEvents.DocumentOpened
CheckAndInsertCopyright(document)
End Sub
It will get called whenever a document opens. I have attached my
CheckAndInsertCopyright() implementation that inserts the content of a
copyright file into every C# file of a given project at the end of the
posting. This should give you a good starting point.
HTH,
Andy
Imports System
Imports System.IO
Imports System.Text
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module Copyright
Public Sub InsertCopyright()
CheckAndInsertCopyright(DTE.ActiveDocument)
End Sub
Friend Sub CheckAndInsertCopyright(ByVal document As EnvDTE.Document)
ClearOutput()
WriteToOutput("Checking Copyright, Version " + "46")
WriteToOutput("File: " + document.FullName)
WriteToOutput("")
'check if it is a C# file!!
If Not document.FullName.EndsWith(".cs",
StringComparison.OrdinalIgnoreCase) Then
Return
End If
'Check if the project is the NSTL
Dim sln As String = DTE.Solution.FullName
If Not sln.EndsWith("nstl.sln",
StringComparison.OrdinalIgnoreCase) Then
Return
End If
'Check if a copyright is present
If Not Copyright.CheckCopyright(document) Then
'if no, insert it
InsertCopyright(document)
WriteToOutput("-->Copyright Inserted.")
Else
WriteToOutput("-->Copyright OK.")
End If
'check year, if necessary repair it
End Sub
Friend copyrightRegion As String = "#region Copyright © 2003 - {0},
Andreas Mueller"
Friend Function CheckCopyright(ByVal document As EnvDTE.Document)
As Boolean
Dim selection As TextSelection = CType(document.Selection,
TextSelection)
selection.LineDown(True, 5)
Dim s As String = selection.Text()
selection.StartOfDocument()
If s.Contains(String.Format(copyrightRegion,
DateTime.Now.Year)) Then
Return True
End If
Return False
End Function
Friend Sub InsertCopyright(ByVal document As EnvDTE.Document)
Dim selection As TextSelection = CType(document.Selection,
TextSelection)
selection.StartOfDocument()
DTE.UndoContext.Open("NSTL Copyright")
Try
Dim sln As String = DTE.Solution.FullName
Dim copyright As String
Dim templateFileName As String =
Path.Combine(Path.GetDirectoryName(sln), "CopyrightTemplate.txt")
WriteToOutput("Inserting Copyright from" + vbNewLine +
templateFileName)
selection.InsertFromFile(templateFileName)
Finally
DTE.UndoContext.Close()
End Try
End Sub
Friend Sub ClearOutput()
Dim out As OutputWindowPane = GetOutputWindowPane("Copyright
Macro", True)
out.Clear()
End Sub
Friend Sub WriteToOutput(ByVal msg As String)
Dim out As OutputWindowPane = GetOutputWindowPane("Copyright
Macro", True)
out.OutputString(msg + vbNewLine)
End Sub
Private Function GetOutputWindowPane(ByVal Name As String, Optional
ByVal show As Boolean = True) As OutputWindowPane
Dim window As Window =
DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
window.Visible = show
Dim outputWindow As OutputWindow
Dim outputWindowPane As OutputWindowPane
If show Then window.Visible = show
outputWindow = CType(window.Object, OutputWindow)
Try
outputWindowPane = outputWindow.OutputWindowPanes.Item(Name)
Catch e As System.Exception
outputWindowPane = outputWindow.OutputWindowPanes.Add(Name)
End Try
outputWindowPane.Activate()
Return outputWindowPane
End Function
End Module