Search code examples
c#visual-studio-2012visual-studio-templates

Visual Studio 2012 project template custom params & renaming issues


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.

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).