There are companies that constantly churn out visual studio projects that mostly look the same as each other, but with different names. The problem with this is that a lot of hours are spent in setting up each project manually and it takes our attention away from the problems that we want to solve. This guide will demonstrate how to create a simple visual studio project template that can be downloaded by other developers to instantly set up their csproj layout.
After choosing the project of choice to template, we’ll need to replace the names of the namespaces and classes we feel would change with each template deployment. There a bunch of template parameters we can include here, but for this case we only really need one or two.
Namespaces and classes will be replaced like so:
namespace $safeprojectname$ ||
What these replacements essentially do is take the name that is entered at the start of a project and plaster it all over the project or solution wherever the template parameter
$safeprojectname$ exists. Next up is we’re going to see how that takes place.
When all name replacements have been made, we create the template by going to Project -> Export Template -> choose the “Project Template” radio button and Next -> Fill out the template details including the name and click the finish button.
The new template created usually lives in the visual studio folder, in an inner folder named “My Exported Templates”. This can be changed during the previous step.
It comes in a zipped format which consists of a .VSTemplate file and a __TemplateIcon.Ico file, as well as the project files for the template.
Above is an example of what a .VSTemplate file typically looks like when it’s created by the template wizard. There isn’t much on this that I paid attention to except for the Name and Description, I left everything else as it was.
If we wanted to edit a class after templating, we can do it via unzipping the file, editing it and then zipping up again. Always making sure that the .VSTemplate file is in the root of the zip. The places highlighted show the
TargetFileName attribute as well as the
ReplaceParameters attribute (which needs to always be set to true if we want to perform param substitution).
When we’re done editing, we simply take the final zipped version of this template and paste to this path: Visual studio 20XX folder/ Templates/ ProjectTemplates. No need to go into the individual language folders.
Voila, it should show up in the list of project templates. Please ensure that the filters are all defaulted as it is not currently possible to search for it using them, citation here.
Having one project is a fine and dandy idea, however in more realistic situations we tend to have more than one or two projects per solution. Thankfully for us, the visual studio templates also have that ability, but with a few tweaks we need to do from our side.
This is exactly what it says on the tin, for each project, repeat the steps from the previous section (except for uploading the template in the Templates folder in VS). However, instead of using “$safeprojectname$” in the files we need to prefix it with “ext_” so it will become “$ext_safeprojectname$”, this includes the file names too. Please make sure to select the correct project when arriving at the Export Template wizard pane (it should be the last option with a dropdown).
As we can see the two zipped templates have been extracted into their own folders, each containing their own .VSTemplate file as expected. Those individual .VSTemplate files are where we will change one or two things.
The .VSTemplate we will create outside of the extracted files will look like this:
All of the lines that are highlighted in red displays some of the differences between creating .VSTemplate file for a project vs one for a solution (such as this). These changes are required to occur for visual studio to realize that we are looking at a solution consisting of x number of projects. We can see that the type has changed from
ProjectGroup, the template content now contains the
ProjectTemplateLink elements instead of just the simple
ProjectName property is what we want it to be called, the element value is where it is located. The
Name will give a name to our solution as a whole.
To zip all at one go, select all then right click –> Send To -> Compressed (zipped) Folder. A new zipped folder will emerge, name it as appropriate; after which, copy this zipped folder to the usual place where templates go to live happily ever after (Visual Studio (20XX)/ Templates/ ProjectTemplates) and just paste it there. We should then be able to see the template appear in the project creation wizard.
I didn’t realize that probably every single thing can be replaced, I attempted to even replace a string and it worked out! All I did was replace the “world” word with $ext_safeprojectname$, and I guess it replaced it too. [Question] I wonder if it lays the whole setup of the solution and project in a straight line of text or something, and when it comes across the first $ sign it prepares to replace until the next $ line (depending on the param name in between the two signs) 🤷♂️? Something to look into!
Thanks for reading 🙌 stick around for more to explore