views:

614

answers:

2

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"&gt;
  <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 :)

A: 

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

Preet Sangha
That gets the template to show up, but when I insert the template, it isn't working properly. ReplaceParameters is definitely set to true in the auto-generated vstemplate file, but the `$if$` statement and the `$projectname$` declaration in my template are not getting replaced.
DanM
+1  A: 

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.

krystan honour
Thanks @krystan. I ended up giving up, then eventually I changed how I organized my namespaces so I didn't need the template anymore. If the need for a template arises again, I'll definitely pay attention to the spacing of my `$if$` statements.
DanM
no probs @DanThMan.. For the record whilst I was pulling my hair out I came accross your stackoverflow post also and felt glad someone else was finding it harder than it should have been, I remembered it and I currently have 25 working templates :)
krystan honour