Search code examples
c#visual-studiovisual-studio-2022

Visual Studio change default template for New Item


How can I change the default template of right click -> New item (Ctrl + Shift + A). For example I want to change the namespace instead of {} to just namespace name; and some other things like internal sealed class.

I found this path:

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

I changed class.cs but I don't get this when creating a new file:

namespace $rootnamespace$;

internal sealed class $safeitemrootname$
{
    
}

Solution

  • After my test, you can refer to the following steps to achieve the effect you want:

    1: Find this path:

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

    2: Copy this two files: Class.cs and Class.vstemplate to the following directory:

    %USERPROFILE%\Documents\Visual Studio 2022\Templates\ItemTemplates\C#

    3: Open and modify the Class.cs file to :

    namespace $rootnamespace$;
    internal sealed class $safeitemrootname$
    {
    
    }
    

    4: Enter VS and create a console application. Tools > Options, find the following settings, set Namespace Declarations to File Scoped: enter image description here

    5: Close and restart VS, in the console application project just created, right-click the project and create a new item: enter image description here

    It should be noted that if you initially copy and modify the Class.cs file in this path:

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

    the same operation will result in the following phenomenon: enter image description here

    This may be caused by different projects. If you must configure the template in a project like Asp.net core, you can report this problem to DC, where VS developers may help. However, the request will only be considered if it gains enough report.