Search code examples
templatesvisual-studio-2013vsix

Project template: file's replacement with TargetFileName


I'm trying to make a Template where the Project's name user provide on "Add-New project" will affect file name. As discribed here, this should be rather easy. But nothing I do in vstemplate file won't modify the file name, as I thought it should.

vstemplate relevant section:

<Project File="ProjectTemplate.csproj" ReplaceParameters="true">
  <ProjectItem ReplaceParameters="true" TargetFileName="Properties\AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
  <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$Class1.cs" OpenInEditor="true">Class1.cs</ProjectItem>
</Project>

I would expect that after vsix (which referencing the C# project template) installed, and when adding new project with the newly created template, the creation is failed with this error:enter image description here

I also tried to modify only the csproj file, but that was unsuccessful too: it did create a project, but the desired replacement wasn't executed.

Does file renaming with vstemplate is doable??


Solution

  • The csproj-File you are referencing in the Project node in your vstemplate-File needs to be an extra file, not the csproj-File referenced in your template project' solution.

    Also add a reference to $safeprojectname$Class1.cs to the extra csproj-File. Then it should work as expected.

    So the file structure in your template project would be for example:

    /
    -- /Properties
              -- AssemblyInfo.cs
    -- Project.csproj               [The one referenced in your .sln]
    -- ProjectTemplate.csproj       [The one used for replacements]
    -- Class1.cs