There is a (often) overlooked feature available in visual studio which lets you quickly create new projects from a template. I often create small console applications with some basic functionality and sometimes it's tedious to go over some basic steps over and over.
Some axamples that you could create a template for:
- A console application with basic features such as a HttpClient or appsettings.json file.
- A default headless-CMS with entity-framework
- A more advanced default Angular application served with a .Net API
- Create a new project
- Modify the project
- Export as a template
It's important to know what exactly was created by visual studio, so let's take a look at this exported zip file. First of all, the project is located in:
%USERPROFILE%\Documents\Visual Studio 2022\Templates\ProjectTemplates
You will find the newly created .zip file here
For completionists sake, the default visualstudio templates are located in:
C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\ProjectTemplates\CSharp
Let's quickly dive into that .zip file shall we? One thing you can notice is the created
.vstemplate file. It contains the summary and definition of the template you entered. If you have want to change the name/description/icon of the template you can do so here.
- Also note the
TemplateContentfields. This contains a list with all items in your template. The
ReplaceParameterstell the template that these files contain variable which need to be changed upon creating a new project.
First thing you will notice is obviously the namespace:
namespace $safeprojectname$. This is a variable that the template will replace when you create a new project based off of this template.
Open a new Visual Studio window and simply click on
File > New > Project... Your new template should be at the top!
Select it and use it like you would use any of the microsoft default Projects!
Enjoy this time saving trick!
You can find the template code here: https://github.com/dotnetrule/Console-QuickStart