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

 

Advertisements

About Amogh Natu

Technology enthusiast, Associate Consultant @ Microsoft, music lover, love my guitar, Microsoft.NET, ASP.NET, C#.NET Professional.
This entry was posted in General, visual studio and tagged , , , . Bookmark the permalink.

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

Your comments or opinions.....

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s