Click here to Skip to main content
Click here to Skip to main content

Adding Customized Visual Studio Code Generation Templates to the Add new item Dialog Box

, 28 Dec 2003
Rate this:
Please Sign up or sign in to vote.
An article on Adding Customized Visual Studio Code Generation Templates to the Add new item Dialog Box

Introduction

This article will explain how to add a newly created or modified VS Code template to the Add new item Dialog box in Visual Studio IDE.

Background

After having a read through the article written by “sh856531” on “Customizing Visual Studio's Code Generation Templates”, I wanted to create an Interface template to use in my own development.

The Problem

“How do you get the new template to appear when you launch add new item dialog?”

The Solution

We need to modify the VSDir file in the LocalProjectItems folder and subfolders. A VSDir file is a text file with a .vsdir extension that provides information to the Add Item and New Project dialog boxes about how to display its items, such as their names, the order in which they appear, and the icon displayed with them. You can have multiple VSDir files in a single directory, but typically, a single VSDir file contains records for multiple Wizards, folders, and templates. Each record in the file is separated by a new line character, and pipe (|) characters separate the fields in each record. Any optional field for which there is no meaningful data should contain a zero (0) as a placeholder.

The following is an example of a VSDir file [wrapped]:

..\..\CSharpAddClassWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|
  #2245|10|#2262|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4515|0|Class.cs
..\..\CSharpAddInterfaceWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|
  Interface|20|An empty interface declaration|
  {FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4515|0|Interface.cs
..\..\CSharpAddComponentWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|
  #2246|20|#2265|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4517|0|Component.cs
..\..\NewCSharpFile.cs|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|
  #2261|30|#2267|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4516|0|CodeFile.cs
..\..\CSharpAddWinServiceWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|
  #2291|40|#2292|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4520|0|Service.cs
..\..\NewInstaller.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|
  #2269|50|#2270|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4518|0|Installer.cs

Firstly let’s create the new template.

Here are the steps taken:

  1. Locate the VC#Wizards directory in the Visual Studio .NET installation. The default installation location for this is C:\Program Files\Microsoft Visual Studio .NET\VC#\VC#Wizards.
  2. Create a new folder under this directory and name it CSharpAddInterfaceWiz. This name is derived per the convention followed by existing wizards.
  3. Copy the contents of CSharpAddClassWiz to this new folder. This will give you a Templates\1033 subdirectory (along with Scripts\1033 subdirectory also), which contains two files - Templates.inf and NewCSharpFile.cs.
  4. Modify the NewCSharpFile.cs as per your requirements. One way to do this is to build an Interface.cs code file as you need it using Visual Studio .NET and then put that file into this folder. The bold faced items get replaced when the code file is inserted in the project using the wizard. The !output directive is for the wizard engine to output the value of the symbol mentioned. For example, the SAFE_NAMESPACE_NAME is the name of the Project’s Default Namespace.
  5. namespace [!output SAFE_NAMESPACE_NAME]
    {
           /// <summary>
           /// Summary description for [!output SAFE_CLASS_NAME] Interface.
           /// </summary>
           public class [!output SAFE_CLASS_NAME]
           {
    
                  #region [!output SAFE_CLASS_NAME] Properties
    
                  #endregion    
    
                  #region [!output SAFE_CLASS_NAME] Methods
    
                  #endregion    
           }
    }
    
  6. Visual Studio .NET uses files with the extension .vsdir to identify and load these code templates into the add new dialog box. We will modify the .vsdir files in the “C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\CSharpProjectItems\LocalProjectItems\Code” and the “C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\CSharpProjectItems\LocalProjectItems” folders using any text editor.
  7. Open the “C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\CSharpProjectItems\LocalProjectItems\Code\code.vsdir” file you will see this line of code in the file:
  8. ..\..\CSharpAddClassWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|
      #2245|10|#2262|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4515|0|Class.cs
  9. Copy the line and paste it on a new line below and modify the sections that is bold:
  10. ..\..\CSharpAddInterfaceWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|
      Interface|10|An empty interface declaration|
      {FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4515|0|Interface.cs
  11. We add this line to the .vsdir file inside the Code folder because we want this template to be available in the Code section.
  12. Open the “C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\CSharpProjectItems\LocalProjectItems\ LocalProjectItems.vsdir” file you will see this line of code in the file:
  13. ..\CSharpAddClassWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|
      #2245|20|#2262|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4515|0|Class.cs
  14. Copy the line and paste it on a new line below and modify the following:
  15. ..\CSharpAddInterfaceWiz.vsz|{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|
      Interface|20|An empty interface declaration|
      {FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}|4515|0|Interface.cs
  16. Open the following folder C:\Program Files\Microsoft Visual Studio .NET 2003\VC#\CSharpProjectItems and create a file named CSharpAddInterfaceWiz.vsz. Now locate the CSharpAddClassWiz.vsz file open this file and copy the data to your newly created file. We will now modify the data in our new file named CSharpAddInterfaceWiz.vsz to reflect the following:
  17. VSWIZARD 7.0
    Wizard=VsWizard.VsWizardEngine.7.1
    Param="WIZARD_NAME = CSharpAddInterfaceWiz"
    Param="WIZARD_UI = FALSE"
    Param="PROJECT_TYPE = CSPROJ"
  18. The line Wizard=VsWizard.VsWizardEngine.7.1 tells Visual Studio if it is version 2002 or 2003.
  19. Now we should be ready to start our Visual studio and use our new Interface template.
  20. Here is an explanation of the fields in a VSDir file:
      Field Meaning
      RelPathName Required. The name of the Wizard's.vsz file, such as MyWizard.vsz.
      {clsidPackage} Optional. A GUID representing a product (such as Visual C++) that has a DLL containing localized resources. Normally, this field is blank for VSDir files that correspond with third-party Wizards.
      LocalizedName Optional. This is the localizable name of the Wizard or template and the name that appears in the Add Item dialog box. This can be a string or a resource identifier of the form #ResID.
      SortPriority Required. An integer representing the sort order and relative priority of the Wizard, with 1 being highest. For instance, if this item is "1," then this will appear next to other 1s and ahead of all 2s or lower.
      Description Required. A localizable description of the template or Wizard as it will appear in the Add Item dialog box when the item is selected. This can be a string or a resource identifier of the form #ResID. Applies only to template files, not folders.
      DLLPath or {clsidPackage} Required. Specifies a full path to a DLL or EXE file, or a GUID of a product that has a .dll file that contains an icon to load for the Wizard. The icon is loaded as a resource out of a DLL/EXE file using the given IconResourceId. This setting overrides {clsidPackage}, if specified, for icon location. Applies only to template files, not folders.
      IconResourceId Optional. A resource identifier within the DLL file that determines the icon to display. If no icon is defined, the environment substitutes the default icon for a file with the same extension as the item. Applies only to template files, not folders.
      Flags Required. See Flags description and table below. Applies only to template files, not folders.
      SuggestedBaseName Required. The default name for the Wizard, displayed in the Name field in the dialog box. This is either a string or a resource identifier of the form #ResID. If the name is not unique, the environment appends the name with an integer. For example, MyFile.asp might be changed to MyFile1.asp. If no name is provided, then "Project" is used. Applies only to template files, not folders.

      Notes

      1. Any non-required field for which there is no meaningful data should contain a 0 (zero) as a placeholder.
      2. If no localized name is provided, the relative path name is used
      3. If no icon is defined, the IDE substitutes the default icon for a file with that extension.
      4. If no suggested base name is provided, "Project" is used.

    And there you have it, the new code template available in the Add new item dialog box. Note that this item is also available in the root project folder.

    This is what the code looks like after it was generated in the code editor:

    History

    • Version 1.0
    • Version 1.1 - Added a step missed in the original draft.

    Conclusion

    This is my first article and I hope everybody will find it helpful. Cheers, from down unda...

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Maruis Marais Work
    Web Developer
    New Zealand New Zealand
    Well, I'm a software developer since 1996, originally coding VB but recently converted to C# (How could I have ever lasted so long way down the alphabet). Born on the tip of Africa but the past two years have been Residing in New Zealand. The African continent got to savage for me, thus I decided that I will trade it for some laid back Island living down under.... My interests include rugby (pity about the Pom’s taking the Ellis cup from our grasp), Counter Strike (gotcha) and programming (any, but enjoy extending the IDE). O yeah, I enjoy sampling and drinking beer from across the world, especially Belgium and English ales…
     
    That’s me Smile | :)

    Comments and Discussions

     
    Generalattaching refernece assemblies with the file Pinmemberswarmt16-Jun-05 15:26 
    GeneralCopyright violation PinmemberGFeldman25-Apr-05 2:09 
    GeneralWonderful article PinmemberPhan Dung24-Jan-05 20:42 
    Generalvsdir files Pinmemberrichard nixon9-Nov-04 19:18 
    GeneralNice article, but for the sake of consistency PinmemberCoskun Oba29-Jan-04 17:19 
    GeneralBeer Pinmemberklenne22-Jan-04 4:20 
    GeneralI need someone to help me learn how to program in vb PinsussGerald Gunter13-Jan-04 5:28 
    QuestionAnybody want to team up and write a custom template editor? Pinmemberjt42030-Dec-03 5:40 
    Generalcustom templates for asp.net Pinmemberrugha29-Dec-03 21:16 
    GeneralRe: custom templates for asp.net PinsussAnonymous29-Dec-03 23:45 
    GeneralRe: custom templates for asp.net PinsussBNikhil11-Jun-04 2:50 
    GeneralRe: custom templates for asp.net Pinmemberrichard nixon9-Nov-04 19:12 
    GeneralRe: custom templates for asp.net PinmemberRichard Nixon9-Nov-04 19:13 
    Questiongood article do you know how to add your own form to the templates? Pinsussvbnetuk28-Dec-03 3:57 
    First of all
    I like your article
     
    I was wondering whether you could help
    I am trying to add my own customized form to a template.
    I have read few articles but still not one clear article where describes
    how to add your own form to the templates.
     
    Anybody any ideas?
     
    Thanks A lot
    Gabriel

    AnswerRe: good article do you know how to add your own form to the templates? PinmemberMaruis Marais Work28-Dec-03 10:02 
    GeneralRe: good article do you know how to add your own form to the templates? Pinsussvbnetuk29-Dec-03 8:58 
    QuestionMissing step? Pinmemberworldspawn18-Dec-03 15:25 
    AnswerRe: Missing step? PinmemberMaruis Marais Work21-Dec-03 9:06 
    GeneralYou beat me to it PinmemberM.Lansdaal18-Dec-03 10:06 
    GeneralQuestions PinmemberJonathan de Halleux17-Dec-03 2:04 
    GeneralRe: Questions PinmemberStephane Rodriguez.17-Dec-03 4:02 
    GeneralRe: Questions PinmemberJonathan de Halleux18-Dec-03 0:34 
    GeneralInstaller required PinmemberStephane Rodriguez.17-Dec-03 1:31 
    GeneralRe: Installer required PinmemberMaruis Marais Work17-Dec-03 10:42 
    GeneralRe: Installer required PinmemberM.Lansdaal18-Dec-03 9:57 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Mobile
    Web01 | 2.8.140721.1 | Last Updated 29 Dec 2003
    Article Copyright 2003 by Maruis Marais Work
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid