Visual Studio macro to add file headers
Microsoft's StyleCop analyzes C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project.
One of these rules is #SA1633. This rule requires that each of your files have a file header that contains a copyright XML element. Of course, you can disable this rule if you want through StyleCop settings. But just in case you have a similar rule at your work (or your own personal rule) or need to adhere to this rule, I'll describe the situation and provide the macro below.
SA1633: FileMustHaveHeader
Cause
A C# code file is missing a standard file header.
Rule Description
A violation of this rule occurs when a C# source file is missing a file header. The file header must begin on the first line of the file, and must be formatted as a block of comments containing Xml, as follows:
//-----------------------------------------------------------------------
// <copyright file="NameOfFile.cs" company="CompanyName">
// Company copyright tag.
// </copyright>
//-----------------------------------------------------------------------
The rule means that you need a header that looks like the above XML comment code. I've made a simple Visual Studio Macro using VBA to perform the insertion of the code.
Adding the Macro:
- Click "Tools" menu
- Click "Macros" menu item
- Click "Macros IDE..." menu choice
- Right click on "My Macros"
- Click "Add" menu item
- Choose "Add Module..." (a module is a VB term for a static C# class with all static members)
- Enter the name of "AddClassHeaderText" (or whatever you want to name your module)
- Paste the code below and update any variables to match your organization or name
Sub FileHeader() Dim doc As Document Dim docName As String Dim companyName As String = "Company Name" Dim authorName As String = "My Name" Dim copyrightText As String = "Copyright statement" ' 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() ' Write first line DTE.ActiveDocument.Selection.LineUp() DTE.ActiveDocument.Selection.Text = "// --------------------------------" DTE.ActiveDocument.Selection.NewLine() ' 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>" DTE.ActiveDocument.Selection.NewLine() ' Could write <email></email> tag ' Could write <date></date> tag ' Could write <summary></summary> tag ' Write last line DTE.ActiveDocument.Selection.Text = "// ---------------------------------" DTE.ActiveDocument.Selection.NewLine() End Sub
* Note: You'll need to remove those two line wraps above. The code was given hard line breaks in the middle of the statements for better blog formatting.
Macro Results
This creates the following text inserted into the top of the source code document:
// ---------------------------------
// <copyright file="MyClass.cs" company="My Company">
// Copyright statement
// </copyright>
// <author>My Name</author>
// ---------------------------------
Adding Macro to Toolbar
To add this to a toolbar in your IDE:
- Click "Tools" menu
- Click "Customize..." menu choice
- Activate "Commands" tab
- Choose "Macros" category
- Select "MyMacros.AddClassHeaderText.FileHeader" (or whatever you named it)
- Drag command to your toolbar.
- You can customize the icon, text, visibility of text/icon, etc. by right clicking on the toolbar button
- Close customization dialog
Improvements
You could further enhance this macro to get the currently logged in user name and company name without needing to hard code those values into the macro.

November 1st, 2008 - 03:47
thx for posting this
May 5th, 2009 - 11:29
Good article, thanks for it!
June 8th, 2009 - 06:19
Very helpfull, thanx for sharing this!
July 10th, 2009 - 02:03
Hi Jim!
Great post.
Thank you very much!
September 30th, 2009 - 01:29
Thanks Jim,
This is very useful.
I am getting some Problem in calling Macro
Problem: I want , when I add a class, then macro should write header and footer. At Present , when I press macro command in toolbar then it write hear and footer.
If you help me on this, that would be great help for me.
Thanks in advance
October 6th, 2009 - 14:11
chandrapal singh,
It sounds like you need to just edit your class template files. These are the templates used when you click “add”->”class”. These template store the initial values used when the class document is initially created.
There’s lots of resources online that talk about how to edit your default template files. So you should be able to find out the information via Google, etc.
May 5th, 2010 - 08:51
Hi Jim,
I want to put all the methods in a particular region.
e.g:
#region Private methods
privatre void func1()
{
}
privatre void func2()
{
}
#endregion
This also applicable for Public methods too. So let me know how can i get all the methods for current Active document and their complete body part so that I can put in the region.
May 5th, 2010 - 09:24
That question has nothing to do with this post.
Check out Regionerate @ http://rauchy.net/regionerate, using that tool you can set up templates like that and apply them to the entire class.
June 1st, 2010 - 12:52
Hi Jim,
You indicated that this code could be integrated into an msbuild project. What is the call from an msbuild project (*.build) to have this code used?
Thanks,
June 1st, 2010 - 15:45
You can run Stylecop as part of msbuild, not this macro.
June 1st, 2010 - 15:50
Sorry for my misunderstanding.
I interpreted the statement “integrated into an MSBuild project” as indicating that a person might extend the macro to provide output (as email or whatever) from an msbuild project.
February 20th, 2011 - 22:02
Is there a simple way to detect that the required text is already there? I am actually looking to run this as a script for all my source files, and don’t want to insert the same text twice. Or the correct header was already added by another author.
Thank you
May 18th, 2011 - 11:24
Very well written. Gives a basic knowhow about macros