Arian Kulp's Blog
opinion, insight, and occasional code

Exporting a project template (Visual Studio 2005)

Monday, January 30, 2006 9:49 PM

I spent way too many hours exporting some projects in Visual Studio over the past few days.  By default, in the File menu, you click Export Template to start the process of exporting your current project, or an item in the project, as a redistributable template.  As it turns out, in Visual Studio Team System, this menu option isn't there.  I thought maybe it hadn't made it in the final RTM build, but then after doing some Googling I found the obvious solution -- customize the menus!  Here are the easy-to-follow steps that I should have thought of on my own:

  1. In Visual Studio, right-click on a toolbar (or the blank area surrounding a toolbar)
  2. Click Customize...
  3. Click Rearrange Commands
  4. Click Add
  5. In left-hand pane, expand File, in right-hand pane click "Export Template"
  6. Click OK
  7. Click Close twice.
  8. Smile!  The option is there now.

What burned the most time was the fact that a C# project wouldn't build when created from the template.  Many, many errors.  It turns out that the problem was the namespace.  All files were templated with the namespace from the original project.  This was not good.  Though class files can be in any namespace, the default namespace for a project is based on the project name.  This still shouldn't matter unless you then add a new file to the project (it would then be in the default namespace, which would be different than the rest).  The problem was all global references to entities like Resources (the “globals::” notation).  For some reason, the original namespace did not show up in the auto-completion for globals, and thus it wouldn't compile.  I had to perform a search-and-replace across every .cs file (including *.Designer.cs).  That itself wasn't hard, but I kept thinking that I had made the necessary changes.  I didn't realize every place the reference was, so I did it by hand at first.  Tediously testing after every change.  Arrgghh!

The simple solution is to go through your solution and replace the namespace with $safeprojectname$ instead.  This is replaced with the new default namespace name when the project is created from template.  The downside is the original project will no longer work, so you need to do this on a copy.  I'd love to understand why the Export Template feature doesn't just manage this using the same mechanism as for refactoring.  It would have been so much less painful.

By the way, VB seems to not have this problem.  My VB project worked first try.  Slightly more information on parameter replacement in templates can be found at http://msdn2.microsoft.com/en-us/library/ms185311.aspx.  There's also a great new article from MSDN Magazine, found at http://msdn.microsoft.com/msdnmag/issues/06/01/CodeTemplates/default.aspx.  Working with .vstemplate and .vsi files can give you great options for distribution of learning code for an article, class, or other such venue.  It just needs a little more documentation (this new article helps), and it takes some practice!

Comments have been closed on this topic.