visual-studio 在 Visual Studio 中创建新类时如何将其默认为 public?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/700086/
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 you default a new class to public when creating it in Visual Studio?
提问by Guy
When I right click in the Solution of a C# Visual Studio project and select Add... > Class... it creates a class without a public modifier. How do I get Visual Studio (2008) to default the class to a public class?
当我右键单击 C# Visual Studio 项目的解决方案并选择添加...> 类...时,它会创建一个没有公共修饰符的类。如何让 Visual Studio (2008) 将类默认为公共类?
采纳答案by Mitch Denny
You need to modify the file located in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033. 
您需要修改位于C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033.
Visual Studio 2010 (10.0) and below:There is a zip file in there called Class.zip. Unpack this, edit the file to put in your publickeyword then re-pack it (make sure you backup the original).
Visual Studio 2010 (10.0) 及更低版本:其中有一个名为 Class.zip 的 zip 文件。解压此文件,编辑文件以放入您的public关键字,然后重新打包(确保备份原始文件)。
After this, make sure VS rebuilds it's cache (which is just the zipfiles unzipped into directories in C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache) by opening a Visual Studio command shell and execute the following command:
在此之后,C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplatesCache通过打开 Visual Studio 命令外壳并执行以下命令,确保 VS 重建它的缓存(这只是解压缩到 .
devenv /installvstemplates
Visual Studio 2012 (11.0) and up:See the answer by @JochemKempe, as it's much easier to change this now, just by editing a single file (no unzipping or rezipping).
Visual Studio 2012 (11.0) 及更高版本:请参阅@JochemKempe 的答案,因为现在更改它要容易得多,只需编辑单个文件(无需解压缩或重新压缩)。
UPDATE:Don't forget to open your preferred text editor with admin privileges before you do any edit.
更新:在进行任何编辑之前,不要忘记使用管理员权限打开您喜欢的文本编辑器。
回答by JochemKempe
In VS2012it's as easy as going to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Classand editing the Class.cs file. 
在VS2012 中,它就像转到C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class和编辑 Class.cs 文件一样简单。
No need to unzip or rebuild cache. A very positive change if you ask me.
无需解压缩或重建缓存。如果你问我,这是一个非常积极的变化。
VS2015: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
VS2015:C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
VS2017(RC): C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
VS2017(RC):C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
VS2017(Professional): C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
VS2017(专业):C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
VS2019 (Enterprise): C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2019(企业版):C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2019 (Professional): C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2019(专业):C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
Outdated
过时的
VS2019 (Preview): C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2019(预览版):C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
回答by Aaron Hoffman
To create a Public class by default for Visual Studio 2012:
默认情况下为 Visual Studio 2012 创建一个 Public 类:
Edit this file:
编辑这个文件:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code33\Class\Class.cs
To look like this:
看起来像这样:
using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
    public class $safeitemrootname$
    {
    }
}
回答by SliverNinja - MSFT
Here is a re-entrant PS scriptlet that will update the C# base class Item Template. The path changes depending on which version/edition of Visual Studio you use. It also backups the original template in case you want to revert in the future.
这是一个可重入的 PS scriptlet,它将更新 C# 基类Item Template。路径会根据您使用的 Visual Studio 版本而变化。它还会备份原始模板,以防您将来想恢复。
$csharpClassTemplates = @("C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code33\Class\Class.cs",
                          "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplatesCache\CSharp\Code33\Class\Class.cs",
                          "C:\Program Files (x86)\Microsoft Visual Studio17\Community\Common7\IDE\ItemTemplates\CSharp\Code33\Class\Class.cs")
$csharpClassTemplates | % {
    Write-Output("Updating template $_")
    Copy-Item $_ -Destination "$_.backup"
    (get-content $_).Replace('public ', '').Replace('class ', 'public class ') | Set-Content $_
 }
回答by marc_s
You could either create your own project template, or modify the existing one. All these project files are template-driven, so you can alter them and/or add your own.
您可以创建自己的项目模板,也可以修改现有的项目模板。所有这些项目文件都是模板驱动的,因此您可以更改它们和/或添加自己的文件。
Check out these links:
查看这些链接:
Marc
马克
回答by Justin Lessard
In addition of JochemKempe great answer, here is how to do it for the .NET core templates.
除了 JochemKempe 很好的答案,这里是如何为 .NET 核心模板做这件事。
.NET Core
.NET 核心
Instead of editing the templates inside the \CSharp\directory, you need to edit the ones inside \AspNetCore\.
\CSharp\您不需要编辑目录中的模板,而是需要编辑\AspNetCore\.
VS2019 (Enterprise): C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\AspNetCore\Code\1033\Class\Class.cs
VS2019(企业版):C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\ItemTemplates\AspNetCore\Code\1033\Class\Class.cs
VS2019 (Professional): C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\AspNetCore\Code\1033\Class\Class.cs
VS2019(专业):C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\AspNetCore\Code\1033\Class\Class.cs
回答by Julo
The answer from JochemKempeworks fine, but is a little hard, since you need to rewrite protected files in Program Files.
JochemKempe 的答案工作正常,但有点困难,因为您需要在Program Files.
Only to update the answer. Templates for Community edition are in the folder:
只为更新答案。社区版的模板位于以下文件夹中:
c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ProjectTemplates
c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ProjectTemplates
Another solution
另一种解决方案
But there is another possibility to create new templates. Copy the files from the source folder to user template folder.
但是还有另一种创建新模板的可能性。将文件从源文件夹复制到用户模板文件夹。
e.g. for Windows Form application it can be one of these source folders (for Visual Studio 2017 Community):
例如,对于 Windows 窗体应用程序,它可以是这些源文件夹之一(对于 Visual Studio 2017 社区):
c:\Program Files (x86)\Microsoft Visual Studio17\Community\Common7\IDE\ProjectTemplates\CSharp\Windows Root\Windows33\WindowsApplication\
c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ProjectTemplates\CSharp\Windows Root\Windows33\WindowsApplication\
c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ProjectTemplatesCache\CSharp\Windows Root\Windows33\WindowsApplication\
The default user template folder is (for Visual Studio 2017 Community):
默认用户模板文件夹是(对于 Visual Studio 2017 社区):
C:\Users\<username>\Documents\Visual Studio 2017\Templates\ProjectTemplates\
And the destination folder for new Windows Form Application template would be:
新的 Windows 窗体应用程序模板的目标文件夹将是:
C:\Users\<username>\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#\Windows33\MyWindowsApplication\
With this you will have two "Windows Form Application" when creating new project. The distinction between these two is only in the default file name. To make a better distinction it is possible to change the displayed name of the template. To change the template name, update the .vstemplatefile.
有了这个,您将在创建新项目时拥有两个“Windows 窗体应用程序”。这两者之间的区别仅在于默认文件名。为了更好地区分,可以更改模板的显示名称。要更改模板名称,请更新.vstemplate文件。
The original csWindowsApplication.vstemplatecontains line:
原始csWindowsApplication.vstemplate包含行:
    <Name Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="2318" />
To change the name you need to change the line to something like this:
要更改名称,您需要将行更改为如下所示:
    <Name ID="2318">My Windows Form Application</Name>
The negative part of this solution is, that you have to rename your new template and you add a new template to existing templates (the old 'incorrect' example will remain and you can still unwillingly use it).
此解决方案的不利部分是,您必须重命名新模板并将新模板添加到现有模板(旧的“不正确”示例将保留,您仍然可以不情愿地使用它)。
But there is also a good part. You do not need to be administrator to add or update the example. (There is no need to rewrite files in Program Filesfolder.)
但也有很好的部分。您无需成为管理员即可添加或更新示例。(无需重写文件Program Files夹中的文件。)

