How to create a custom project template for Visual Studio?

How to create a custom project template for Visual Studio?
Photo by freestocks / Unsplash

Although I primarily work on web projects, I do create many console applications, too. I often write them to assist me in the development process (e.g. to test code snippets or even to develop whole services before implementing them in the web project), but also to automate boring, repetitive tasks like ...

  • Building and deploying my static website.
  • Synchronizing app permissions with the Customer Identity (CIAM) provider.
  • Generating bearer tokens (for third-party APIs).
  • Generating whole C# classes (code generation).
  • Calculating the weights for my next training session. 💪

Sometimes it is great to have additional base functionality like dependency injection and being able to read configuration data from an appsettings.json. Unfortunately, this does not come out-of-the-box with Visual Studio's default "Console App" template. Instead, you need to add the functionality yourself.

So, why not automate the creation of those enhanced console applications, too? – Visual Studio gives you the possibility to export every project as a project template. This is a great starting point that seems much easier than creating the whole template yourself.

On GitHub, you can find a repository of mine, which includes the features mentioned above. To create a project template from it, just follow these steps:

  1. Clone or download the repository.
  2. Open the solution in Visual Studio (2022).
  3. Click on the "Project" menu and choose "Export Template...".
  4. Choose template type "Project template" (default).
  5. On the "Template Options" page, you can keep the default settings (see below). If you like, you can add a nice description and an icon.
"Template Options" page with default settings.

Afterwards, your new project template should show up in Visual Studio. Unfortunately, you might quickly notice that it is missing the usual tags like "C#", "Console" and so on. To fix that, you could manually insert the following tags into the file "MyTemplate.vstemplate" (which is part of the zipped project template and should be located in "%USERPROFILE%\Documents\Visual Studio 2022\Templates\ProjectTemplates"):

<TemplateData>
    <LanguageTag>csharp</LanguageTag>
    <PlatformTag>windows</PlatformTag>
    <PlatformTag>linux</PlatformTag>
    <PlatformTag>macos</PlatformTag>
    <ProjectTypeTag>console</ProjectTypeTag>
</TemplateData>
💡
Tip: After making the manual changes, I needed to move the zipped template to the "Visual C#" subfolder and restart Visual Studio (before the changes became visible). – If it still does not work, delete the following folder and restart Visual Studio: "%LOCALAPPDATA%\Microsoft\VisualStudio\<YOUR_VS_VERSION_NUMBER(e.g.'17.0_e623138a')>\ProjectTemplatesCache_{00000000-0000-0000-0000-000000000000}".

The final project template then should look something like this:

Screenshot of the Visual Studio (2022) project template selector.
💡
By the way: The icon used for this project template is created with the awesome favicon generator "Favicon.io".

Share this post: