Cool Visual Studio 2017 Tip #4 : Templates

I was recently watching an interesting C#7 video by Jesse Liberty and noticed that within it, he was using his own project type to demonstrate the new features of the language. That seemed really useful so here I’m going to show you how you can do something similar using existing features of Visual Studio. I’ll be using version 2017 but it wouldn’t surprise me if this existed in prior versions too.

OK, so what do I want to achieve? I’d like to create a project which is console based, uses the .NET framework and comes with a baked in class called Runner to execute code, much like Jesse’s. I also want to hook everything together so that I can run it immediately if I choose to. The first step is to create something just like that but here’s mine in case you’re wondering:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;

namespace ConsoleRunner
{
    class Program
    {
        static void Main(string[] args)
        {
            Runner run = new Runner();
            run.Run();
        }
    }

    public class Runner
    {
        public void Run()
        {
            Console.WriteLine("This is the run method.");
        }
    }
}

Don’t forget to save your project before you move onto the next step.

  1. Under the Project menu, choose Export Template…
  2. Leave the choice of Project Template selected and click “_Next >_”
  3. On the screen that appears, give the template a name, add a description and if you like, choose icon and preview images. Everything else can be left as is. In my case, I named my project as ConsoleRunner so I didn’t want to change that name.
  4. Click Finish to generate your template.

Where are the templates stored?

After clicking Finish, you will be presented with the folder in which the template has been added. On my machine that’s here:

C:\Users\[user name]\Documents\Visual Studio 2017\My Exported Templates

As you can see, the template is stored as a zip file, but what’s inside?

Now, when you go to create a project, you have this new option:

And if you click on it, you can see the description you may have added:


Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com