How do I edit the Visual Studio templates for new

2019-01-02 16:31发布

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?

4条回答
余生请多指教
2楼-- · 2019-01-02 17:09

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#.

查看更多
梦寄多情
3楼-- · 2019-01-02 17:18

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.

查看更多
梦该遗忘
4楼-- · 2019-01-02 17:20

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

查看更多
唯独是你
5楼-- · 2019-01-02 17:25

Extract, edit and recompress. Paths are for the class template, but the 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.

2005:

C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip

2008:

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

2010:

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

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

2012:

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

2013:

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

2015:

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

2017

VS 2017 changes the directory location, and is now dependent on your edition (Professional/Enterprise/etc). So for the Enterprise edition:

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

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:

C:\Program Files (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.

查看更多
登录 后发表回答