Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


C# (C sharp): Microsoft FAQ

Managing Projects & Solutions

C# Singleton template for VS 2003 by chiph
Posted: 16 Jul 06

I found myself creating a large number of Singleton objects recently, and after cutting/pasting (and making the usual cut & paste coding mistakes) I decided to create a Visual Studio template for the process.

In your C# folder (located at C:\Program Files\Microsoft Visual Studio .NET 2003\VC# on my machine), go into the CSharpProjectItems folder and create a new file named CSharpAddSingleton.vsz  In this file, put this code:


Param="WIZARD_NAME = CSharpAddSingleton"
Go back to your VC# folder and create a new folder named CSharpAddSingleton.  Create two new folders in there, named Scripts and Templates.

Under the Scripts folder, create a folder named 1033,  create a file named default.js.  In this file, put this code:


// CSharp add-in wizard for creating singletons
function OnFinish(selProj, selObj)
    var oldSuppressUIValue = true;
        var strTarget = wizard.FindSymbol("ITEM_NAME");
        var strClassName = strTarget.split(".");
        var bValid = wizard.ValidateCLRIdentifier(strClassName[0]);
        if (!bValid)
        oldSuppressUIValue = dte.SuppressUI;
        var strProjectName = wizard.FindSymbol("PROJECT_NAME");
        var strSafeProjectName = CreateSafeName(strProjectName);
        wizard.AddSymbol("SAFE_PROJECT_NAME", strSafeProjectName);
        var strProjectPath = wizard.FindSymbol("TARGET_FULLPATH");
        var strTemplatePath = wizard.FindSymbol("TEMPLATES_PATH");

        var strTpl = "";
        var strName = "";
        var InfFile = CreateInfFile();

        AddFilesToCSharpProject(selObj, strProjectName, strProjectPath, InfFile, true);
        if( e.description.length > 0 )
        return e.number;
           dte.SuppressUI = oldSuppressUIValue;
           if( InfFile )

function SetFileProperties(oFileItem, strFileName)
This javascript code gets called by the IDE to customize the template that we're creating next.

Go to the Templates folder we created earlier, and create a 1033 folder, and in that folder, we'll create two files.

First one is NewCSSingleton.cs, which will be the code you get out of the wizard, containing several replacement tags:


using System;

namespace [!output SAFE_NAMESPACE_NAME]
    /// <summary>
    /// Summary description for [!output SAFE_CLASS_NAME].
    /// </summary>
    public sealed class [!output SAFE_CLASS_NAME]
        static readonly [!output SAFE_CLASS_NAME] _instance = new [!output SAFE_CLASS_NAME]();

        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static [!output SAFE_CLASS_NAME]()

        [!output SAFE_CLASS_NAME]()

        public static [!output SAFE_CLASS_NAME] Instance
                return _instance;
The next one is Templates.inf, which tells Visual Studio the name of the template file.  It contains:


Now for the complicated part.
Go to the VC# folder, then follow down the CSharpProjectItems folder and the LocalProjectItems folder.  Create a new file in this directory called CSharpAddSingleton.vsdir, which is what Visual Studio uses to display and locate the other files.

In the CSharpAddSingleton.vsdir file, we'll create several items which are separated with vertical pipe (|) characters.
  1. The relative path to the .vsz file
  2. {FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}
  3. C# Singleton Class
  4. 0
  5. A Singleton Class
  6. {FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}
  7. 4534
  8. 0
  9. Singleton.cs
This should look like the following (all on one line, in case your browser wraps this text):


..\CSharpAddSingleton.vsz|{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}|C# Singleton Class|0|A Singleton class|{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}|4534|0|Singleton.cs
And that should do it.  Now when you open Visual Studio 2003, and create a new project, and select "Add new item..." from the File menu, you should see your new singleton template in the list.  Select it, and it will create a new file called Singleton1.cs in your project.

Read more at:

Chip H.

Back to C# (C sharp): Microsoft FAQ Index
Back to C# (C sharp): Microsoft Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close