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?
10 Answers
In VS2012 it's as easy as going to C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class and editing the Class.cs file.
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
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
VS2019 (Enterprise): 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
Starting with Visual Studio 2022 VS is a 64 bit application, meaning the item templates are in C:\Program Files\... instead of C:\Program Files (x86)
VS2022 (Community): C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2022 (Professional): C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2022 (Enterprise): C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2025 (Community): C:\Program Files\Microsoft Visual Studio\18\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2025 (Professional): C:\Program Files\Microsoft Visual Studio\18\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
VS2025 (Enterprise): C:\Program Files\Microsoft Visual Studio\18\Enterprise\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
16 Comments
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.csYou need to modify the file located in 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 public keyword then re-pack it (make sure you backup the original).
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:
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).
UPDATE: Don't forget to open your preferred text editor with admin privileges before you do any edit.
4 Comments
To create a Public class by default for Visual Studio 2012:
Edit this file:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\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$ { } } Comments
In addition of JochemKempe great answer, here is how to do it for the .NET core templates.
.NET Core
Instead of editing the templates inside the \CSharp\ directory, you need to edit the ones inside \AspNetCore\.
VS2019 (Enterprise): 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
Comments
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:
- MSDN on VS Project Templates
- Create your own VS Project Templates
- CodeProject on VS Project Templates
Marc
Comments
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.
$csharpClassTemplates = @("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\ItemTemplatesCache\CSharp\Code\1033\Class\Class.cs", "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs") $csharpClassTemplates | % { Write-Output("Updating template $_") Copy-Item $_ -Destination "$_.backup" (get-content $_).Replace('public ', '').Replace('class ', 'public class ') | Set-Content $_ } 1 Comment
..\2017\Professional\... and I edited it manually but this showed the new folder structure for 2017. Thanks.The Everything program is very nice for finding files on the computer.
I used this filter to find all the Class.cs files
CSharp\Code\1033\Class\Class.cs
Just putting this out there because finding the files seems like a painful part of solving this problem.
So you can use the Everything program to find the file and then edit it as suggested elsewhere.
Comments
The answer from JochemKempe works fine, but is a little hard, since you need to rewrite protected files in 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
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):
c:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ProjectTemplates\CSharp\Windows Root\Windows\1033\WindowsApplication\ c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ProjectTemplates\CSharp\Windows Root\Windows\1033\WindowsApplication\ c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ProjectTemplatesCache\CSharp\Windows Root\Windows\1033\WindowsApplication\ The default user template folder is (for Visual Studio 2017 Community):
C:\Users\<username>\Documents\Visual Studio 2017\Templates\ProjectTemplates\ And the destination folder for new Windows Form Application template would be:
C:\Users\<username>\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#\Windows\1033\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 .vstemplate file.
The original csWindowsApplication.vstemplate contains line:
<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 Files folder.)
