DEV Community

Roel
Roel

Posted on • Edited on

How to create a project template for visual studio

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:

  1. A console application with basic features such as a HttpClient or appsettings.json file.
  2. A default headless-CMS with entity-framework
  3. A more advanced default Angular application served with a .Net API

How to create a template

  1. Create a new project
  2. Modify the project
  3. Export as a template Image description
  4. Select 'Project Template' and press Next. Now enter a name and a description. I really suggest using an icon which represents what you have build. I will use a CMD icon I found on the internet.
    Image description

  5. Click finish

What did it create?

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


Enter fullscreen mode Exit fullscreen mode

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


# The generated template #
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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svcq34r4nhvhyebfv2jr.png)
* Also note the `TemplateContent` fields. This contains a list with all items in your template. The `ReplaceParameters` tell the template that these files contain variable which need to be changed upon creating a new project.

Let's look at one of the `.cs` files, the `startup.cs`:
![bmp icon cmd](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5w7r1v5chv2kuo23gvad.png)

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.



# How to use the template #
Open a new Visual Studio window and simply click on `File > New > Project..`. Your new template should be at the top!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylqezqtrx79xer5tdmph.png)

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

src: https://docs.microsoft.com/en-us/visualstudio/ide/how-to-create-project-templates?view=vs-2022 

Enter fullscreen mode Exit fullscreen mode

Top comments (0)