Hero image!

Create multi-project templates in Visual Studio

July 15, 2023 - Drygast
Basics C# How To Visual Studio

Do you find yourself creating the same type of basic solution/projects over and over? If so - you might want to use a multi project template to speed things up. Personally I create a lot of basic console app projects that always contain a main console project, a class library and a test project and while it's not exactly difficult to do, it's easier to use a template.

Create solution and projects

To create a template, you will need a solution and 2 or more projects to start with. I created these by using the usual "Create Project" guide in Visual Studio and added projects to a solution. I started with a console app that I named "TemplateConsoleApp", added a class library "TemplateConsoleAppLibrary" and a xunit test project "TemplateConsoleAppTest". I also added referencess to the necessary projects and ended up with something that looked like this.

Template solution and projects

I also added some basic code to be able to run a simple test

// AppLibrary.cs

namespace TemplateConsoleAppLibrary
{
    public class AppLibrary
    {

        ...

        public string Ping()
        {
            return "Pong";
        }
    }
}

// PingTests.cs
namespace TemplateConsoleAppTest
{
    public class PingTests
    {
        [Fact]
        public void PingAppLibrary()
        {
            var appLibrary = new TemplateConsoleAppLibrary.AppLibrary(Array.Empty<string>());
            string result = appLibrary.Ping();

            Assert.Equal("Pong", result);
        }
    }
}

I could go on and write a lot more, but this basic setup is enough to continue.

Templates

To create templates, simply go to the menu and click "Project / Export Template...".

Export Template

The guide is pretty self-explaining BUT you have to do this for all 3 projects - it cannot be done for a solution.

Export Template guide page 1
Export Template guide page 2

Now we should have 3 zip files in the export folder "My Exported Templates.".

My exported templates folder

We have to create a new folder in the same "My Exported Templates." folder. I called mine "MyConsoleApp". Now we have to extract the 3 zip files for our exported projects into this new folder.

Extract into new template folder

We have to create a template file in this folder. This is just a text file with the .vstemplate file extension.

MyConsoleApp.vstemplate

MyConsoleApp.vstemplate

<VSTemplate Version="2.0.0" Type="ProjectGroup"
    xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">
    <TemplateData>
        <Name>My Console App</Name>
        <Description>An example of a multi-project template</Description>
        <Icon>Icon.ico</Icon>
        <ProjectType>CSharp</ProjectType>
    </TemplateData>
    <TemplateContent>
        <ProjectCollection>
            <ProjectTemplateLink ProjectName="TemplateConsoleApp">
                TemplateConsoleAppMyTemplate.vstemplate
            </ProjectTemplateLink>
            <ProjectTemplateLink ProjectName="TemplateConsoleAppLibrary">
                TemplateConsoleAppLibraryMyTemplate.vstemplate
            </ProjectTemplateLink>
            <ProjectTemplateLink ProjectName="TemplateConsoleAppTest">
                TemplateConsoleAppTestMyTemplate.vstemplate
            </ProjectTemplateLink>
        </ProjectCollection>
    </TemplateContent>
</VSTemplate>

Select the 3 folders and the .vstemplate file and compress into a .zip file by right-clicking on the .vsstemplate file (while folders are also selected) and selecting the option to compress into a .zip file. This should have created a new file called MyConsoleApp.zip in that folder.

Compress files and folders

Move the MyConsoleApp.zip file to the project templates folder. (user / document / Visual Studio 2022 / Templates / ProjectTemplates )

Project templates folder

That's it. You should now be able to use the template from Create a new Project guide in Visual Studio.

Testing it

Select the "Create a new Project" option when starting Visual Studio and search for the name that you gave the template in previous steps.

My Console App template is there!

Create the project as normal.

Create project

And there we have it. My very own project created using the template.

Project created from template

Conclusion

While this might not be necessary for the majority of developers, I found this somewhat useful. I do create a lot of small basic console projects and using a template really makes the process a lot quicker. There is ofcourse the option to use .NET Core CLI to create the solution and projects as well, but the template way also allows for some basic code to be included. Anyway - a fun little times-saver or completely useless? You decide. :)