Jakew
Consulting, hacking, and motorcycles

Visual Studio Templates

Saturday, 8 March 2008 08:40 by jakew

Maybe it’s just me with my head buried in work but I’ve really not noticed people singing the praises of VS’s templates feature as much as I would think they should. If you are not familiar with them you can read about them here. The VS docs are good, a tad bland and generic but they explain what can be done.

In case you don’t have time to read everything I’ll provide some details. But first I want to explain why I think this feature is important. As a developer I produce code. That is what developers do, or should be doing. I have particular ways that I like to have things done, the boiler plate stuff that goes in to every project, and class file I write. Having templates around saves me time. Without the template stuff I end up having to put in the stuff I like by hand. Worst case I’m doing a cut & paste job all over the place. Prior to templates I wrote Macros. Actually, I’m not whining about macros b/c macros let you do cool stuff. But for now just focusing on adding a class to a file VS’s templates rock!

So how do you make your own template and then how do you make it do the cool stuff that makes it special? I’ll drill in to that in a couple of steps. The first step will be generating our first version of the template. Then we will customize the template to do some cool stuff.

Generating a template

To get started open Visual Studio and create a new C# class library project. Visual Studio creates all the usual stuff you are used to. Go to the solution explorer and select class1.cs and open it. You should see this:

clip_image002

Everything you see in the solution and in the editor came from a template. We will use this as the starting point for out template. First, I’m going to add a file header (yeah, I like those) and a bunch of regions. It will now look like this:

clip_image004

I’m happy with this setup; this is how I like my class files to look. The only problem right now is that if I make a template out of this the namespace will be wrong and the class will be wrong. I need to tell Visual Studio to substitute the correct values for those. For an added bonus I’d like the date to be put in the header (less work for me). Visual Studio provides a few default parameters to do this. You can read the docs here. In this case we need three parameters: safeitemname, safeprojectname, and time. To use the parameters in the template you put the parameter inside a pair of dollar signs. The template will now look like this:

clip_image006

Now we are ready to export the template. For convenience we will export the template twice. First we will export a project template and then we will export an item template. Exporting the template is really easy: go to File/Export Template. If the menu item is not on the menu go to Tools/Customize and add it.

First export the project template. Click File/Export Template:

clip_image008

Select Project and click Next.

clip_image010

Fill in the template name, description and optionally put in an icon. Leave the check boxes checked so Visual Studio will automatically add the templates for use. Click Finish. Visual Studio export the template to “My Exported Templates” Under the Visual Studio directory in your “My Documents” area. Just copy the project template over to the Templates directory (in my case “My Documents\Visual Studio 2005\Templates\Project Templates\Visual C#”. Once you’ve got the template file where it goes go create a new project. When you go to File\New Project in Visual Studio you should see:

clip_image012

“Extended Library” is my template. Notice the sweet icon I made for it. This will let us create a class library just the way we like it. Now repeat the process and export the class file as an item template. It’s the same process as the project, but this time switch to item template in the first page of the wizard. After you complete the project, copy the template over to your C# item templates library. Now you can add your customized class to your customized project and have everything match.

These are small improvements, but even so it saves me a little time (30 seconds maybe) each time I add a new project or class to a project. You can also add references and stuff to your project so that those will be included each time you start a new project. For instance if you use EntLib in your project you can create a template that already references the parts of EntLib you use. That way when you create a project and add classes to it they already have the references you need in place.

What is in the template file?

So far we’ve been able to do everything by generating our templates from within Visual Studio. What if you want to either hand craft your template or edit an existing template? No problem. Visual Studio’s templates are just ZIP files with a few items inside. The most important file inside the template has the extension .vstemplate. There can be only one and VS looks for it when you run the template. Inside the vstemplate file is XML that describes the template and tells VS what to do in order to expland it. The XML contains things like the name to display in the new file dialog, the type of project, the icon to display (if any). The most important thing in the XML is the section that tells VS what files and directories to add. A Project template can contain an entire directory structure if needed. An Item template will only contain a few files (think about partial classes and resources). The XML also allows you to reference executable code to be run as part of the template. I’ll get in to that later. But for a full run down on the Template XML go here. Looking at the schema it is apparent that I’m only scratching the surface in this article. You can do a lot inside your template to help yourself.

Besides the VSTemplate file there will be the actual template files that you have generated. You can just drag files in to the template and then reference them inside the VSTemplate and Visual Studio will take care of putting them where you want them.

Finally, you can throw an icon in the zip file and reference it in the XML and VisualStudio will display your icon in the new file/project dialog. In some ways this is a trivial thing, but it also helps because you can more quickly identify your template. If you already have an icon you like laying around then use it. If not, don’t waste too much time making one.

Up to this point we have been throwing all of our templates in to your “My Documents” folder. Most of the time this is a good idea and keeps things simple. This approach even works great when giving your friends your favorite template. Just have them throw it in the MyTemplates folder and be done with it. However, eventually you might want to make your template look like it shipped as part of Visual Studio. To do that you need to put your templates where Visual Studio keeps its templates. Again, MSDN tells you all about it. Go here.

Once you are happy with your template copy it to either:

· <VSinstall dir>\Common7\IDE\ItemTemplates\(Language)\(Locale)

· <VSinstall dir>\Common7\IDE\ProjectTemplates\(Language)\(Locale)

In my case, being a C# bigot in the US the directories would be:

· \Program File\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033

· \Program File\Microsoft Visual Studio 8\Common7\IDE\ProjectTemplates\CSharp\Windows\1033

The same stuff works for Visual Studio 2008, just change the directories. After you copy the template to the directory you will need to delete the contents of the cache and tell visual studio to rebuild the cache. Skipping this step will prevent you from seeing your shiny new template.

To make your template show up exit Visual Studio, go to <VSInstalldir>\Common7\IDE\ItemTemplatesCache and delete all of its contents. Then at the command line run “deveenv /installvstempltes” you might have to run the visual studio command prompt to make it work, just go to the Visual Studio menu in your start menu.

Conclusion

I feel that I’ve barely scratched the surface of the possibilities that Visual Studio’s templates represent. Even so, what I’ve covered can have a significant impact on your productivity. Begin creating templates for common elements you use in your everyday work and reap the benefits.

Categories:  
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed