Search code examples
c#visual-studiovisual-studio-2010templatesvisual-studio-templates

Adding custom format to a template parameters Visual studio project


I have defined a Visual Studio template called classDB.cs. I would like the default name for the class to appear as [projectname]DB.cs, where [projectname] is the name of the current project (as entered in the Create Project dialog). Is there a way to achieve this? I tried setting the name of the class to $safeprojectname$DB.cs, but that didn't work.

UPDATE

I modified my project template but give's this error when it's generating the project

enter image description here

here's the template class

namespace $safeprojectname$.Models
{
    public class $safeprojectname$DB : DbContext
    {

    }
}

the class in the project


Solution

  • I have been battling with a similar error to this for days, and I finally figured it out. Visual Studio escapes the $ in the .csproj file. So you will have a node that looks like this:

    <Compile Include="Models\%24safeprojectname%24DB.cs" />
    

    Open up the .csproj file in a text editor, and change it to:

    <Compile Include="Models\$safeprojectname$DB.cs" />
    

    And save the file. Your project will reload, but it won't try to escape the filename again! Export your template, and you should find that the parameter now gets replaced.