0

I'm trying to make a simple proof of concept type visual studio template, but I can't seem to get the built in variables to work like they should. I'm trying to follow the documentation here https://learn.microsoft.com/en-us/visualstudio/ide/template-parameters?view=vs-2019 but it's not working.

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
  <TemplateData>
    <Name>Readers</Name>
    <Description>&lt;No description available&gt;</Description>
    <ProjectType>CSharp</ProjectType>
    <ProjectSubType>
    </ProjectSubType>
    <SortOrder>1000</SortOrder>
    <CreateNewFolder>true</CreateNewFolder>
    <DefaultName>Readers</DefaultName>
    <ProvideDefaultName>true</ProvideDefaultName>
    <LocationField>Enabled</LocationField>
    <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
    <Icon>__TemplateIcon.ico</Icon>
  </TemplateData>
  <TemplateContent>
    <Project TargetFileName="Readers.csproj" File="Readers.csproj" ReplaceParameters="true">
      <ProjectItem ReplaceParameters="true" TargetFileName="App.config">App.config</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="DBAccess.cs">DBAccess.cs</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.cs">MainReader.cs</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="packages.config">packages.config</ProjectItem>
      <Folder Name="Properties" TargetFolderName="Properties">
        <ProjectItem ReplaceParameters="true" TargetFileName="AssemblyInfo.cs">AssemblyInfo.cs</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="Resources.resx">Resources.resx</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="Resources.Designer.cs">Resources.Designer.cs</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="Settings.settings">Settings.settings</ProjectItem>
        <ProjectItem ReplaceParameters="true" TargetFileName="Settings.Designer.cs">Settings.Designer.cs</ProjectItem>
      </Folder>
      <ProjectItem ReplaceParameters="true" TargetFileName="ReaderBase.cs">ReaderBase.cs</ProjectItem>
    </Project>
  </TemplateContent>
</VSTemplate>

This is what I came up with following that example. This is basically the same template file that was generated when I did Project -> Export Template in visual studio except for the line.

      <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.cs">MainReader.cs</ProjectItem>

which should rename the MainReader.cs file to the name of the project when I make a new one using this template. It does not, whenever I make a new project with this template that file keeps the name MainReader.cs. I've tried adding the ext_ extension to the name, I've tried using the variable inside the brackets instead of outside and both. There has to be something else I'm missing or maybe this just isn't a thing any more. The process I've been using to get the template in is using the export template wizard, extracting that zip file, changing the .vstemplate file, putting the whole thing back in a zip fil, and then finally replacing the zip file in exported templates folder for visual studio. There may be another place I need to put the revised zip file but I'm not sure.

Eric Timms
  • 23
  • 5

1 Answers1

1

So I finally managed to get this to work and it ended up being multiple issues.

Firstly I was actually saving my new template to the wrong place. The exported templates folder is not where visual studio looks for user created templates, this is confusing because it will automatically add any template you export to it's list so you may see this folder full of templates that show up when you're making a new project and think this is where it's pulling it's templates list from. the actual location for user created templates is noted here. https://learn.microsoft.com/en-us/visualstudio/ide/how-to-locate-and-organize-project-and-item-templates?view=vs-2019 in the user templates section. You can also change this location under tools -> options -> projects and solutions -> locations. The correct procedure is to export the template, extract the zip file somewhere you can work on it, make your changes to the vstemplate file and the proj file (I'll get to that in a bit), compress all of that into a zip file, and then place that file in the user templates directory.

Another thing I noticed, and this is specifically when setting the file name to a variable, is that you also have to edit the .csproj file (or any other project file I'm assuming) and change the file name in there to match what is on the .vstemplate file. In my case in order for this to work

      <ProjectItem ReplaceParameters="true" TargetFileName="$safeprojectname$.cs">MainReader.cs</ProjectItem>

my csproj file had to include this

    <Compile Include="$safeprojectname$.cs" />

replacing the old line that listed MainReader.cs. With both of those taken care of I was able to see the file name changed like I expected it to be.

Eric Timms
  • 23
  • 5