DEV Community

Cover image for Visual studio project templates ๐Ÿšš๐Ÿ—
Abdul
Abdul

Posted on

Visual studio project templates ๐Ÿšš๐Ÿ—

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.

Step 1: Picking the parts of the project that will change

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$ || class $safeprojectname$

We can also name the file directly like so:
Naming file directly in solution explorer

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.

Step 2: Creating the template

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.

Step 3: Takingย a look inside the template

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.

Created Template

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.

.vstemplate

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

Using the template

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.

Using the template

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.

Multi-project solution templates

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.

Step 1: Export all the projects

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

Step 2: Extract files of zipped templates

Files Extracted

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.

Step 3: Create an external .VSTemplate

The .VSTemplate we will create outside of the extracted files will look like this:

External .VSTemplate

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 Project to ProjectGroup, the template content now contains the ProjectCollection and ProjectTemplateLink elements instead of just the simple Project element.

The ProjectName property is what we want it to be called, the element value is where it is located. The SolutionFolder property Name will give a name to our solution as a whole.

Step 4: Zip all in one go

Zip all

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.

Solution Template

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

Top comments (2)

Collapse
 
ihtesham_haider profile image
Ihtesham Haider

Nice bruh!

Collapse
 
iamabdul profile image
Abdul

Thanks!