How do you default a new class to public when creating it in Visual Studio?

后端 未结 7 823
不知归路
不知归路 2020-11-28 02:54

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)

7条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 03:14

    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\\Documents\Visual Studio 2017\Templates\ProjectTemplates\
    

    And the destination folder for new Windows Form Application template would be:

    C:\Users\\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:

        
    

    To change the name you need to change the line to something like this:

        My Windows Form Application
    

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

提交回复
热议问题