Question

Task:
I want to create a project template from a Visual Studio 2012 project, so I can quickly create similar projects. This requires files to be renamed and the root namespace to be changed. Unfortunately, this does not happen...

What has been done:

  1. I've created the project and replaced certain names as specified by the MSDN pages. One replacement is the $safeprojectname$, the other is a custom parameter: $custom1$.
  2. The project has been exported as a project template via the File --> Export Template... option.
  3. I've unzipped the created zip file.
  4. I've edited the .vstemplate and .csproject files. The code is found below.
  5. I've rezipped the files.
  6. Visual Studio has been restarted, and a project has been opened.
  7. I go for Add --> New Project --> *my custom template*, give it a name and add the project.
  8. The project gets added and $safeprojectname$ gets replaced. The files are not renamed and $custom1 does not get replaced.

MyTemplate.vstemplate

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
   <TemplateData>
      <Name>MyTemplateName</Name>
      <Description>My Description</Description>
      <ProjectType>CSharp</ProjectType>
      <ProjectSubType></ProjectSubType>
      <SortOrder>1000</SortOrder>
      <CreateNewFolder>true</CreateNewFolder>
      <DefaultName>My Default Name</DefaultName>
      <ProvideDefaultName>true</ProvideDefaultName>
      <LocationField>Enabled</LocationField>
      <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
      <Icon>__TemplateIcon.ico</Icon>
   </TemplateData>
   <TemplateContent>
      <Project TargetFileName="MyTemplate.csproj" File="MyTemplate.csproj" ReplaceParameters="true">
         <ProjectItem ReplaceParameters="true" TargetFileName="app.config">app.config</ProjectItem>
         <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$Data.cs">Data.cs</ProjectItem>
         <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.cs">Main.cs</ProjectItem>
         <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$Form.xaml">Form.xaml</ProjectItem>
         <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$Form.xaml.cs">Form.xaml.cs</ProjectItem>
         <Folder Name="Properties" TargetFolderName="Properties">
            <ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
            <ProjectItem ReplaceParameters="true" TargetFileName="Settings.settings">Settings.settings</ProjectItem>
            <ProjectItem ReplaceParameters="true" TargetFileName="Settings.Designer.cs">Settings.Designer.cs</ProjectItem>
         </Folder>
      </Project>
      <CustomParameters>
         <CustomParameter Name="$custom1$" Value="Some.Custom.Namespace.Prefix."/>
      <CustomParameters>
   </TemplateContent>
</VSTemplate>

MyTemplate.csproj

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
    <PropertyGroup>
        ...
    <OutputType>Library</OutputType>
    <RootNamespace>$custom1$$safeprojectname$</RootNamespace>
    <AssemblyName>$safeprojectname$</AssemblyName>
    </PropertyGroup>
    <ItemGroup>
        <Compile Include="$safeprojectname$.cs" />
        <Compile Include="$safeprojectname$Data.cs" />
        <Compile Include="$safeprojectname$Form.xaml.cs">
            <DependentUpon>$safeprojectname$Form.xaml</DependentUpon>
        </Compile>
        <Compile Include="Properties\AssemblyInfo.cs" />
        <Compile Include="Properties\Settings.Designer.cs">
            <AutoGen>True</AutoGen>
            <DesignTimeSharedInput>True</DesignTimeSharedInput>
            <DependentUpon>Settings.settings</DependentUpon>
        </Compile>
    </ItemGroup>
    <ItemGroup>
        <Page Include="$safeprojectname$Form.xaml">
            <SubType>Designer</SubType>
            <Generator>MSBuild:Compile</Generator>
        </Page>
    </ItemGroup>
    <ItemGroup>
        <None Include="app.config" />
        <None Include="Properties\Settings.settings">
            <Generator>PublicSettingsSingleFileGenerator</Generator>
            <LastGenOutput>Settings.Designer.cs</LastGenOutput>
        </None>
    </ItemGroup>
</Project>

Question?
How do I get Visual Studio to change the $custom1$ parameter in the files and to rename the files, like Main.cs, to $safeprojectname$.cs?

What doesn't work

  • Following the steps on the MSDN pages got me nowhere; neither the filenames change, nor the custom parameter.
  • Searching Google gave me MSDN pages, or references to them.
Was it helpful?

Solution

I don't know how visual stuido handels exported templates, but i know from modifing the default templates that visual studio has two versions of a template. One you will find in the "ProjectTemplate" folder the other will be in "ProjectTemplateCache".

Maybe your exported template is still inside the cach without your modifications. Try to refresh the cache by statring visual studio from cmd using the parameter: "devenv /installvstemplates" or "devenv /setup".

If you want to create more than one template, I recommend to download the Visual Studio and use a VSIX project to depoly the templates. The SDK also allow to debug the templates in a much more efficent way. You can press Debug and you will recive a Visual Studio instance with a installation of your VSIX (your templates).

OTHER TIPS

Your steps and project files look correct. You need to update the Visual Studio Template Cache. The answer in How to update Visual Studio Template Cache links to the steps.

Here are the steps that worked for me (note your user name will be different):

  1. Close all open Visual Studio instances.
  2. Edit the copy of your template located in C:\Users\chadd\Documents\Visual Studio 2013\My Exported Templates - follow steps in above in OP.
  3. Search to find other copies of your Template and delete them. I have Visual Studio 2013 and found copies in these two directories. C:\Users\chadd\AppData\Roaming\Microsoft\VisualStudio\12.0\ProjectTemplatesCache C:\Users\chadd\Documents\Visual Studio 2013\Templates\ProjectTemplates
  4. Copy the edited version of yourTemplate.zip into C:\Users\chadd\Documents\Visual Studio 2013\Templates\ProjectTemplates
  5. Open a command prompt for Visual Studio (Run as Administrator) and enter devenv /installvstemplates This step takes a while (several minutes) so be patient for the command prompt to return.
  6. Open Visual Studio verify your modified template is now working.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top