如何为新的 C# 类/接口编辑 Visual Studio 模板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2072687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I edit the Visual Studio templates for new C# class/interface?
提问by J?rn Schou-Rode
I find myself removing the following import statements in nearly every C# file I create in Visual Studio:
我发现我在 Visual Studio 中创建的几乎每个 C# 文件中都删除了以下导入语句:
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.
当然,使用 Resharper 很容易做到这一点,但我真的不应该这样做。
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?
VS 目录中的某处必须有一些模板(类、接口),我可以从中删除违规行。我在哪里可以找到这些文件?有没有更好的方法来控制默认导入列表?
采纳答案by Sam Harwell
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.
您可能希望编辑每个中的 VS 模板文件,以消除它们不会自动添加对程序集 System、System.Data 和/或 System.Xml 的引用这一事实。
2005:
2005年:
C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip
C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip
2008:
2008年:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
2010:
2010年:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
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.
从 Visual Studio 2012 开始,模板不会被压缩,因此您可以.cs
直接编辑每个适用文件夹中的每个模板。
2012:
2012年:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
2013:
2013年:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
2015:
2015年:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
2017
2017年
VS 2017 changes the directory location, and is now dependent on your edition (Professional/Enterprise/etc). So for the Enterprise edition:
VS 2017 更改了目录位置,现在取决于您的版本(专业版/企业版/等)。所以对于企业版:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
2019
2019年
The VS 2019 location is similar to 2017. So for the Enterprise edition:
VS 2019 位置与 2017 类似。因此对于企业版:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
C:\Program Files (x86)\Microsoft Visual Studio\2019\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:
在 Express Editions 中,您必须WDExpress
在IDE
文件夹内的子目录中进行搜索,例如对于 VS 2015 Express:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\WDExpress\ItemTemplates\CSharp\Code\1033\Class\Class.cs
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.
如果您不使用英文版的 Visual Studio,则该文件夹1033
可能不存在,而是代表您的语言的不同数字。例如,它1031
在德国安装中。
回答by SLaks
You're looking for the following directory:
您正在寻找以下目录:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp
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.
每个模板都是此文件1033
夹中某个类别的(英文)子文件夹中的一个ZIP 文件。您可以编辑.cs
ZIP 文件中的文件。
If you're on a 32bit system, remove the (x86)
. VS2005 is Microsoft Visual Studio 8
, and VS2010 is Microsoft Visual Studio 10.0
.
如果您使用的是 32 位系统,请删除(x86)
. VS2005 是Microsoft Visual Studio 8
,而 VS2010 是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#
.
请注意,这些模板不是针对每个用户的。您可以通过将这些 ZIP 文件复制到My Documents\Visual Studio 2008\Templates\ItemTemplates\Visual C#
.
回答by Aaron Hoffman
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):
对于 Visual Studio 2012,要从默认的 class.cs 文件中删除这些,项目模板可能如下所示(不要包含/删除 using 语句):
Default Class Template file: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
默认类模板文件: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
更多信息:http: //aaron-hoffman.blogspot.com/2013/05/edit-default-visual-studio-2012-item.html
回答by tjlindahl
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
新的类模板也可能位于不同的文件夹中,具体取决于项目类型。在 VS 2015 中的 Web 项目中工作时,我发现在编辑文件时
C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
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
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.
我能够使用我想要的默认格式创建一个新类。
回答by TiTime
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"
Productivity Power Tools(visual studio extension,无论如何我都建议使用它)-> 启用名为“Remove and Sort Usings on save”的选项/设置
No extra click / keyboard shortcuts / manual template modification etc ... since you have to save your files anyways.
没有额外的点击/键盘快捷键/手动模板修改等......因为你无论如何都必须保存你的文件。
回答by sunefred
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:
只想补充 Sam Harwell 的出色回答。可能有多个类模板,例如AspNetCore
在以下位置有自己的模板:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\AspNetCore\Code\1033\Class\Class.cs
C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\AspNetCore\Code\1033\Class\Class.cs
回答by Dave Robinson
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:
Sam Harwell 提供的优秀信息的新增内容。我使用的是 Visual Studio 2019 社区,我的类模板的路径与前面提到的相同,除了“企业”,它被“社区”代替。所以:
C:\Program Files (x86)\Microsoft Visual Studio19\Community\Common7\IDE\ItemTemplates\CSharp\Code33\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.
还值得一提的是,“zip”类文件夹在我的安装中根本不是 zip。它只是另一个文件夹。
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 Studio19\Preview\Common7\IDE\ItemTemplates\CSharp\Code33\Class
For other templates, the folder to look for is:
对于其他模板,要查找的文件夹是:
C:\Program Files (x86)\Microsoft Visual Studio\[YEAR]\[EDITION]\Common7\IDE\ItemTemplates
C:\Program Files (x86)\Microsoft Visual Studio\[YEAR]\[EDITION]\Common7\IDE\ItemTemplates
So, on my machine it is:
所以,在我的机器上是:
C:\Program Files (x86)\Microsoft Visual Studio19\Preview\Common7\IDE\ItemTemplates
[Edit 10 May 2020]
[编辑 2020 年 5 月 10 日]
You also need to check the value of "User item templates location" in Tools | Options | Project and Solutions / Locations
您还需要检查工具 | 中“用户项目模板位置”的值。选项 | 项目和解决方案/地点
I hope this helps someone.
我希望这可以帮助别人。