Add file header to new class/other items automatically on creation in Visual Studio

Hi,

This post shows how we can make little changes to the Class template in visual studio so that we can add a file header to a newly created class. It’s generally a good practice to add a file header to any class/file in a solution but it’s equally a pain when we have to copy-paste a file header from a different file and remember to change the name at the top. Instead you can just follow the below steps to make Visual Studio add it automatically for you with the right filename when you create a new class.

Note: I’ve done this for Visual Studio 2015. But it is pretty much the same for other versions

  1. Navigate to the below folder
    C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\
  2. Open the Class.cs file in notepad or any editor you find comfortable. Edit it as shown below. Basically just add the file header you wish to add to every class. But edit the filename to $safeitemrootname$.cs. This will ensure that the name of the file you’ve provided is replaced here.
  3. Make any more changes as required. Save.
  4. Go back to visual studio to check if it is actually working.
  5. Modify other templates like for Interface or any other file type if required. Note :  you might want to check the folders at the 1033 level in the above path.

Hope this helps!

[Update]

The below post by Abhijit Jana shows very clearly how the same can be achieved with more controls using Macros. Do have a look.

Add document header for files automatically in Visual Studio

 

4 thoughts on “Add file header to new class/other items automatically on creation in Visual Studio

    1. Thanks for pointing me to it! I didn’t know about Macro. In fact I’m new to it. Let me try this out; looks interesting. Also, do you mind if I update the post and add a link to this post of yours?

Leave a Reply