Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
28 Dec 2003 1  
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