How to change the default C++ template file?

5k Views Asked by At

I'm using Visual Studio 2019, and whenever I create a new C++ project it gives me a default file with the following code:

// Template Test.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

int main()
{
    std::cout << "Hello World!\n"; 
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

This is so much unnecessary information and it takes a minute to change it to what I really want;

// Template Test.cpp : This file contains the 'main' function. Program execution begins and ends there.


#include <iostream>

using namespace std;

int main() {
    cout << "Hello World!\n"; 
}

It doesn't take very much time, but I have to do it every time I create a new C++ console project in VS 19.

After doing a quick search on my computer I found a folder called 'Templates' at the following location:

C:\Users\yale\Documents\Visual Studio 2019\Templates

This file has subdirectories that would lead me to think it's the right place, but none of the folders have any template files as I can see.

How can I modify the template files for different projects in C++ with Visual Studio 2019?

1

There are 1 best solutions below

1
On BEST ANSWER

There are two ways you can choose any of them.

Use the Export Template Wizard:

Visual Studio provides an Export Template Wizard that can be used to update an existing template:

  • Choose File > New > Project from the menu bar.
  • Select the template that you want to update and continue through the steps to create the new project.
  • Modify the project in Visual Studio. For example, change the output type or add a new file to the project.
  • On the Project menu, choose Export Template. The Export Template Wizard opens.
  • Follow the prompts in the wizard to export the template as a .zip file.
  • (Optional) Place the .zip file in the following directory: %USERPROFILE%\Documents\Visual Studio \Templates\ProjectTemplates to make it available for selection. You'll need to perform this step if you did not select the option Automatically import the template into Visual Studio in the Export Template Wizard.
  • Delete the old template .zip file.

To manually update an existing template:

  • Locate the .zip file that contains the template. User project templates are located at %USERPROFILE%\Documents\Visual Studio \Templates\ProjectTemplates.
  • Extract the .zip file.
  • Modify or delete the current template files, or add new files to the template.
  • Open, modify, and save the .vstemplate XML file to handle updated behavior or new files.
  • Select the files in your template, and from the right-click or context menu, and choose Send to > Compressed (zipped) folder. The files that you selected are compressed into a .zip file.
  • Put the new .zip file in the same directory as the old .zip file.
  • Delete the extracted template files and the old template .zip file.

Also, you can visit the source page for more details.