170

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?

1
  • 1
    You need to change the class template, as described here. It's slightly more involved than just editing a single file (which is what you might expect) but it doesn't sound too bad. Commented Mar 31, 2009 at 5:23

10 Answers 10

255

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

Sign up to request clarification or add additional context in comments.

16 Comments

This doesn't seem to be working for me. I even replaced the Class.cs file in the ItemTemplatesCache directory as well. Rebooted VS2012 each time. Is there a way to rebuild the cache maybe?
For VS2013 use this location: C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
You may need to have your text editor open in Admin mode, in order to update the actual class template.
For VS2015 use this location: C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class However, if you have Xamarin installed, you might also need to do a search on you windows drive for "Class.cs". I ended up just changing all of the results to public.
Visual Studio 2019 is out now so Preview should change to indicate the version. I only have Professional installed but I guess the other two would be Community and Enterprise VS2019 Professional: C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class\Class.cs
|
87

You 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

Big Thank you. On a 64 bit Windows 7 Machine with VS 2012 this path is at C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp
what if you want change the item template between a team on different machines, any ideas?
In VS2013, you'll need to run that Developer Command Prompt for VS2013 as Administrator, and run the command after closing Visual Studio - it won't complete while VS is open.
No need to visit the blog post @Guy referenced, as the post has been edited to include the information found there. If you want to visit that blog post, it has now moved to a new URL: guyellisrocks.com/2009/03/…
19

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

8

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

7

In VS 2022 Professional, for those who just want to edit the Class.cs file and move on, it's changed paths given that 2022 is 64 bit and is no longer in the x86 path. Now it's:

C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class 

Comments

4

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

Comments

4

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

My path was ..\2017\Professional\... and I edited it manually but this showed the new folder structure for 2017. Thanks.
3

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

enter image description here

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

2

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

Comments

2

In Visual Studio 2022 you can go to this location and edit the Class.cs file.

C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.