Question

I'm trying to create my own class template for Visual Studio called "Public Class".

I followed the official MSDN instructions on how to manually create an Item Template pretty much to the letter, but I'm having no luck getting the template to show up in the "My Templates" section of the "Add New Item" dialog.

I placed PublicClass.zip in the following Windows XP directory:

C:\Documents and Settings\My User Name\My Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C#.

Public Class.zip contains the following files:

  1. PublicClass.cs
  2. PublicClass.ico
  3. PublicClass.vstemplate

PublicClass.cs contains...

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ == 3.5)using System.Linq;
$endif$using System.Text;

namespace Pse.$projectname$
{
    public class $safeitemname$
    {
    }
}

PublicClass.vstemplate contains...

<?xml version="1.0" encoding="utf-8"?>
<VSTemplate
  Type="Item"
  Version="1.0.0"
  xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
  <TemplateData>
    <Name>Public Class</Name>
    <Description>An empty class declaration with access modifier = public and namespace = Pse.NameOfProject</Description>
    <Icon>PublicClass.ico</Icon>
    <ProjectType>CSharp</ProjectType>
    <DefaultName>PublicClass.cs</DefaultName>
  </TemplateData>
  <TemplateContent>
    <ProjectItem ReplaceParameters="true">PublicClass.cs</ProjectItem>
  </TemplateContent>
</VSTemplate>

I started up Studio, tried to add a new item to my project, but my template was nowhere to be found. So, I followed the advice of this stackoverflow question, which was to call...

devenv.exe /installvstemplates

...from the command line, but all that did was make all the templates (including the built-in ones) disappear. After a few moments of panic, I eventually got them back after running the command several times, but that clearly wasn't the answer for me.

If you see anything wrong with my procedure here, can you please help me out?

Thanks in advance.

EDIT:

If I use File/Export Template (as suggested by Preet), the template does show up in My Templates, but the parameters are not replacing properly.

If I insert a new "Public Class", this is what I get:

using System;
using System.Collections.Generic;
$if$ (3.5 == 3.5)using System.Linq;
$endif$using System.Text;

namespace Pse.$projectname$
{
    public class PublicClass1
    {
    }
}

So, only $projectname$ and $targetframeworkversion$ are replacing properly. It's not handling the $if$ statement or $projectname$ properly. (I also tried using $safeprojectname$, but that made no difference.)

This is definitely one of those cases where trying to make my life a little more convenient had had exactly the opposite effect :)

Was it helpful?

Solution

I had this problem :)

And I think I managed to solve it I was doing something very similar what I really wanted was to have a standard template which had headers in for our svn system I ended up extracting the existing Class.zip file in Foo:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033 and then editing it I ended up with this

// $Id$
// $Author$
namespace $rootnamespace$
{
    using System;
    using System.Collections.Generic;
$if$ ($targetframeworkversion$ == 3.5)  using System.Linq;
$endif$    using System.Text;

    public class $safeitemrootname$
    {
    }
}

The spacing for the $if$ statement did seem to make a difference If I had tabs or spaced before them they would not evaluate properly or rather they did evaluate as you are seeing but were simply printed in my code.

As for your templates disappearing when you run /installvstemplates, well whats happening is you may notice that cmd takes quite a while to run, what it is infact doing is deleting Foo:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache and then rebuilding that folder by extracting the zip files in the original locations as mentioned above. For this command to behave visual studio is best shutdown whilst you run it and you should wait until the command has finished to restart it. (tip I used to actually monitor the folder and watch it build).

Personally allthough this is a flexible system microsoft really could have made this easier for the user, after all R# has this as a simple right click feature, if you have R# this is the easiest way to implement templates outside of the ms system.

OTHER TIPS

If you want it to work in Visual Studio 2008 you have to change <VSTemplate Version="1.0.0"> to <VSTemplate Version="3.0.0"> in your .vstemplate file. Then place the .zip file in what you have designated as your user item templates location.

I had problems with the manually created ZIP file. Unless I did an export of an existing template and changed it I'd never get the template to appear. These are the instructions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top