19 Jul 2010

Use a Visual Studio Macro to Insert Copyright Headers into Source Files

In work today, I needed to add a copyright header to a large number of C# classes.  Instead of simply pasting the header across the 20+ files, I took a look at the various tooling options.

The Code Header Designer looked like a possible option. However, I wanted something lightweight and easily configurable, so I settled on using a VS macro to insert the headers.  A quick Google revealed a number of example macros already written to do exactly what I required.  This is my version of James Welch’s code:

 

I then followed the instructions in this article to add a context menu entry to call the macro by right-clicking the target file.  This resulted in the following header comment being inserted in each file:

   1:  // <copyright file="Sample.cs" company="My Company Name">
   2:  // Copyright (c) 19/07/2010 23:01:05 All Right Reserved
   3:  // </copyright>
   4:  // <author>Andy Parkhill</author>
   5:  // <date>19/07/2010 23:01:05 </date>
   6:  // <summary>Class representing a Sample entity</summary>

It is worth noting that by taking 20 minutes to research the possible options, I was able to find modify an existing macro that is significantly faster than copying and pasting boilerplate text across the various files, and that can be modified for other uses.

If I had to do this on a new project, I would instead create a custom class template specifically for the project with required header and class structure.

1 comment:

  1. hey andy i`ll follow u from now!!
    i`d like learn C# visual studio :D!!

    ReplyDelete

Note: only a member of this blog may post a comment.