258

I find myself removing the following import statements in nearly every C# file I create in Visual Studio:

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

Of course its really easy to do this with Resharper, but I really should not have to.

There must be a few template (class, interface) somewhere in the VS directory, from which I can remove the offending lines. Where do I find these files? Is there a better way to control the default import list?

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
Jørn Schou-Rode
  • 37,718
  • 15
  • 88
  • 122

8 Answers8

463

2022

Visual Studio 2022 is now a 64bit process so its location has changed to using Program Files instead.

%ProgramFiles%\Microsoft Visual Studio\<year>\<edition>\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.cs

Year Edition Absolute path
2022 Community %ProgramFiles%\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Enterprise %ProgramFiles%\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Professional %ProgramFiles%\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Preview %ProgramFiles%\Microsoft Visual Studio\2022\Preview\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs

2017 through 2019

Visual Studio 2017 changed the directory location to use release year instead of its internal version number, and is additionally dependent on your edition (Professional/Enterprise/etc.):

%ProgramFiles(x86)%\Microsoft Visual Studio <year>\<edition>\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.cs

Year Edition Absolute path
2019 Community %ProgramFiles(x86)%\Microsoft Visual Studio 2019\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Enterprise %ProgramFiles(x86)%\Microsoft Visual Studio 2019\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Professional %ProgramFiles(x86)%\Microsoft Visual Studio 2019\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
2017 Community %ProgramFiles(x86)%\Microsoft Visual Studio 2017\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Enterprise %ProgramFiles(x86)%\Microsoft Visual Studio 2017\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Professional %ProgramFiles(x86)%\Microsoft Visual Studio 2017\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs

2012 through 2015

Starting with VS 2012, the templates are not zipped, so you can edit each .cs template in each applicable folder directly.

%ProgramFiles(x86)%\Microsoft Visual Studio <version>\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.cs

Year Version Absolute path
2015 14.0 %ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.cs
2013 12.0 %ProgramFiles(x86)%\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.cs
2012 11.0 %ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\1033\Class.cs

2010 and older

Extract, edit and recompress the ZIP file. Paths are for the class template, but interface templates are in the same folder.

You may want to edit the VS template file in each to remove the fact that they don't automatically add references to the assemblies System, System.Data and/or System.Xml.

%ProgramFiles(x86)%\Microsoft Visual Studio <version>\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip

Year Version Absolute path
2010 10.0 %ProgramFiles(x86)%\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
2008 9.0 %ProgramFiles(x86)%\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
2005 8 %ProgramFiles(x86)%\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip

Notes

Express Editions

In Express Editions you will have to search in the subdirectory WDExpress inside the IDE folder, so e.g. for VS 2015 Express:

%ProgramFiles(x86)%\Microsoft Visual Studio 14.0\Common7\IDE\WDExpress\ItemTemplates\CSharp\Code\1033\Class\Class.cs

Other Languages

If you don't use the English version of Visual Studio, the folder 1033 may not exist, but a different number representing your language. For example it is 1031 in a German installation.

Markus Jarderot
  • 86,735
  • 21
  • 136
  • 138
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • 45
    It doesn't always reflect these changes in Visual Studio. If it doesn't for you, edit the files in the `ItemTemplatesCache` directory (in addition to `ItemTemplates`). E.g. (in 2005) `C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip\Class.cs`. Note that `Class.zip` here is a directory, not an actual ZIP file. – Joe Jan 15 '10 at 17:14
  • And by the way, this is also the place to change AutoEventWireUp="true" to false in CSharp\Web\1036\WebForm.zip and WebUserControl.zip. Usefull. – Johnny5 Jun 13 '11 at 18:20
  • 7
    Be aware that service pack updates will wipe your cache, so make sure you back it up so you can put it back. – sweetfa Sep 06 '12 at 00:45
  • 3
    @Joe: Or force visual studio to reload the templates by running devenv /setup . It may take a minute for that command to run. – Brian Oct 16 '12 at 14:06
  • Of important note: there are multiple template files to handle different situations (e.g. one for standard `.cs` files, another for WPF Window `.cs` files, another for `aspx.cs` files, etc.), so you can search for `*.cs` in the `ItemTemplates` folder and change each accordingly to change the default behavior in each situation. – Peter Majeed Feb 01 '13 at 19:54
  • 4
    @Brian devenv.exe /installvstemplates is enough. At least for VS 2012. Is executable while VS is running. – Lucas Aug 04 '13 at 03:01
  • 2
    When I attempted to save the edited `Class.cs` in ItemTemplatesCache under VS2010,it says another program has opened it,but I cannot find which one.So I deleted ItemTemplatesCache folder,then extract, edit and recompress,it works! – zionpi Aug 18 '14 at 01:54
  • 6
    If you're using ASP.NET/MVC and Visual Studio won't pick up your updates to the template, try editing `C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\WebClass\Class.cs.` Then run devenv.exe /installvstemplates. – J. Andrew Laughlin Sep 23 '14 at 20:19
  • Don't just rename `Class.zip` I have seen VS2010 pick up the renamed file... very confusing – KCD Jul 06 '15 at 05:04
  • 1
    You can also copy the respective template to your user directory and edit it there. E.g. for VS2019: `%USERPROFILE%\Documents\Visual Studio 2019\Templates\ItemTemplates\Visual C#` – arni Aug 15 '19 at 10:18
  • The correct location VS 2019 version 16.10 (Enterprise): `%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs` – arni Aug 21 '21 at 08:18
  • @arni 1. I have Visual Studio 2019 Enterprise on Win Server 2019. I changed the Class.cs template in the ...\1033\Class folder. Still VS not using edited template. 2. Do I copy the whole '1033' folder or just the 'Class' folder to "UserProfile's \Visual C#\" Folder. Thanks – Mehdi Anis Nov 10 '21 at 17:19
  • I'm trying to update templates to use C# 10's file scoped namespaces, but it still includes brackets. Other changes were applied, so I don't think it's a cache issue. Thoughts? – Joel Christophel Feb 04 '22 at 18:07
  • @JoelChristophel: In an `.editorconfig` file, set the following property: `csharp_style_namespace_declarations=file_scoped:suggestion` – StriplingWarrior Jan 18 '23 at 22:55
9

You're looking for the following directory:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp

Each template is a ZIP file inside the 1033 (English) subfolder of one of the categories in this folder. You can edit the .cs file inside the ZIP file.

If you're on a 32bit system, remove the (x86). VS2005 is Microsoft Visual Studio 8, and VS2010 is Microsoft Visual Studio 10.0.


Note that these templates are not per-user. You can make per-user templates by copying those ZIP files to My Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C#.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Thanks for the per-user option here. Note though, with VS2015 at least, there are no zip files/folders. My paths were `C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class` and then I created and copied to `...\Documents\Visual Studio 2015\Templates\ItemTemplates\Visual C#\Code\1033\Class`. I had Visual Studio closed while I did this. And I also ran the `devenv.exe /installvstemplates` command mentioned in another comment. When I relaunched VS, my changes showed up when creating new classes. Excellent! – sliderhouserules Oct 02 '15 at 20:11
2

New class templates might be located in different folders depending on the project type as well. While working in a Web Project in VS 2015, I found that when editing the file

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs

I was unable to see any difference in the default template when creating a new class. It was only after editing the file

C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\WebClass\Class.cs

that I was able to create a new class with the default format that I wanted.

tjlindahl
  • 21
  • 2
1

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

Default Class Template file: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs

using System;
namespace $rootnamespace$
{
    class $safeitemrootname$
    {
    }
}

More info here: http://aaron-hoffman.blogspot.com/2013/05/edit-default-visual-studio-2012-item.html

Aaron Hoffman
  • 6,604
  • 8
  • 56
  • 61
1

Just want to add to the excellent answer from Sam Harwell. There might be more than one class template, for instance AspNetCore has its own template at:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\AspNetCore\Code\1033\Class\Class.cs

sunefred
  • 3,988
  • 1
  • 11
  • 13
1

New addition to the excellent info from Sam Harwell. I'm using Visual Studio 2019 Community, and the path to my class template is the same as already mentioned, except for "Enterprise", which is replaced by "Community". So:

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

It's also worth mentioning that the Class "zip" folder isn't a zip at all on my installation. It's simply another folder.

Another "gotcha" is that if you are running a Preview edition, it's in it's own "Preview" folder:

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

For other templates, the folder to look for is:

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

So, on my machine it is:

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

[Edit 10 May 2020]

You also need to check the value of "User item templates location" in Tools | Options | Project and Solutions / Locations


I hope this helps someone.

1

None of the mentioned solutions worked for me so I'm posting the way how I have solve it for VS 2019 (16.7.7):

I have followed the steps from here:

  • I have opened the dir with the template:

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

  • Edited the template adjusting it to my needs and saved (Class.cs)

  • Zipped both files: Class.cs and Class.vstemplate

  • Moved the zip to: cd "%USERPROFILE%\Documents\Visual Studio 2019\Templates\ItemTemplates"

  • I'm not sure if it is necessary but I have run devenv /installvstemplates

  • Same as above (not sure it is needed) restarted VS

Max
  • 381
  • 3
  • 6
0

Old question - new answer: Sorry for the, maybe, offending question: Why would you care about it at all.

Productivity Power Tools (visual studio extension, i would recommend using it anyway) -> enable the option / setting called: "Remove and Sort Usings on save"

No extra click / keyboard shortcuts / manual template modification etc ... since you have to save your files anyways.

TiTime
  • 7
  • 1