Coder Perfect

How can I make changes to the Visual Studio templates for a new C# class or interface?


In practically every C# file I build in Visual Studio, I remove the following import statements:

using System.Collections.Generic;
using System.Linq;
using System.Text;

Of course, Resharper makes this quite simple, but I shouldn’t have to.

I’m sure there’s a template (class, interface) someplace in the Visual Studio directory where I can remove the offending lines. What is the location of these files? Is there a method to control the default import list more effectively?

Asked by Jørn Schou-Rode

Solution #1

Because Visual Studio 2022 is now a 64-bit process, its location has been shifted to Program Files.

The directory location in Visual Studio 2017 has changed to use the release year instead of the internal version number, and it is also depending on your edition (Professional/Enterprise/etc.):

The templates are no longer compressed in VS 2012, therefore you can change each.cs template in each corresponding folder individually.

The ZIP file should be extracted, edited, and then recompressed. The paths are for the class template, however the interface templates are in the same folder as the class template.

You could want to change the VS template file in each to remove the fact that they don’t add references to the assemblies System, System automatically. Data and/or System are two terms that can be used interchangeably. Xml

For Express Editions, look in the WDExpress subfolder within the IDE folder, for example, VS 2015 Express:

If you don’t have Visual Studio in English, the folder 1033 may not exist, but a different number reflecting your language may. In a German installation, for example, it is 1031.

Answered by Sam Harwell

Solution #2

The following directory is what you’re looking for.

Program Files (x86) (C:) (x86) (x86) (x86) (x86) (x86) Microsoft Visual Studio 9.0Common7IDEItemTemplatesCSharp Microsoft Visual Studio 9.0Common7IDEItemTemplatesCSharp

Each template is a ZIP file that may be found in the 1033 (English) subfolder of one of the folder’s categories. The.cs file in the ZIP package can be edited.

Remove the 32bit option if you’re using a 32bit system (x86). Microsoft Visual Studio 8 is VS2005, and Microsoft Visual Studio 10.0 is VS2010.

Please keep in mind that these templates are not per-user. By copying the ZIP files to My DocumentsVisual Studio 2008TemplatesItemTemplatesVisual C#, you can create per-user templates.

Answered by SLaks

Solution #3

To delete these from the default class.cs file in Visual Studio 2012, the item template could look like this (do not include/remove using statements):

C:Program Files (x86) contains the default Class Template file. CSharpCode1033ClassClass.cs Microsoft Visual Studio 11.0Common7IDEItemTemplates

using System;
namespace $rootnamespace$
    class $safeitemrootname$
} for more information.

Answered by Aaron Hoffman

Solution #4

Depending on the project type, new class templates may be located in different folders. When I was working on a Web project in Visual Studio 2015, I discovered that when I edited the file

Program Files (x86) (C:) (x86) (x86) (x86) (x86) (x86) Microsoft Visual Studio 14.0Common7IDEItemTemplatesCSharpCode1033ClassClass.cs Microsoft Visual Studio 14.0Common7IDEItemTemplatesCSharpCode1033ClassClass.cs

When establishing a new class, I couldn’t tell the difference between the default and custom templates. It was only after I had finished modifying the file that I realised what had happened.

Program Files (x86) (C:) (x86) (x86) (x86) (x86) (x86) Microsoft Visual Studio 14.0Common7IDEItemTemplatesCSharpCode1033WebClassClass.cs Microsoft Visual Studio 14.0Common7IDEItemTemplatesCSharpCode1033WebClassClass.cs

I was able to construct a new class with the desired default format.

Answered by tjlindahl

Solution #5

Sam Harwell’s great information now has a new element. I’m using Visual Studio Community 2019, and the path to my class template is the same as before, but “Enterprise” has been replaced by “Community.” So:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class

It’s also worth noting that the Class “zip” folder on my system isn’t a zip at all. It’s just a new folder.

Another “gotcha” is that the Preview edition is stored in its own “Preview” folder:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class

The folder to look for alternative templates is:

Program Files (x86) (C:) (x86) (x86) (x86) (x86) (x86) [YEAR][EDITION] Microsoft Visual Studio Common7\IDE\ItemTemplates

So, on my machine, it’s as follows:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\ItemTemplates

[Updated on May 10, 2020]

In Tools | Options | Project and Solutions / Locations, double-check the setting of “User item templates location.”

I hope this information is useful to someone.

Answered by Dave Robinson

Post is based on