Click here to Skip to main content
15,032,967 members
Articles / Containers / Virtual Machine
Posted 20 Apr 2007


13 bookmarked

A simple Switch Framework for the Visual Studio prompt and the devenv command

Rate me:
Please Sign up or sign in to vote.
4.78/5 (11 votes)
20 Apr 2007CPOL3 min read
A library for creating custom switches for the devenv command prompt (extensibility example).

Main Screenshot


The purpose of this simple framework is to develop switches for the devenv command that you can launch from the Visual Studio 2005 prompt. You can do many things from the VS2005 prompt using the devenv command. For example, if you write devenv "name of project/name of solution", without any switch, a new instance of VS is opened with the project/solution loaded. If you write devenv /setup, the environment resources metadata that describes menus, toolbars, and others from VSPackages are merged with the VS environment. Here are all the switches for the devenv command prompt: MSDN.

This framework was born when I had to open a website from a prompt. As far as I know, there isn't an easy way to do this (for example, devenv "website folder"). In the Internet, I found that a method to create a macro, install in VS, and run it with devenv /Command. So, I decided to look for a better way and, in particular, a method that could be useful in future also. Developing a package, for me, was a beautiful idea. So, I searched for some documentation to create custom switches and run them. Here is how to implement custom switches: MSDN.


  1. In order to use the VS 2005 version of the framework, you have to install Visual Studio 2005 SDK.
  2. In order to use the Orcas version of the framework, you have to download Orcas and the Visual Studio Orcas SDK. The Orcas version is the same as the 2005 version, only recompiled. You can download Orcas on a virtual machine (Windows Server 2003) from Microsoft.
  3. A basic knowledge about VSPackage/add-ins.


Here is a class diagram and a sequence diagram to show how the package communicates with the switch framework.

Class Diagram

Class Diagram

Sequence Diagram

Sequence Diagram

How to Develop a Custom Switch

In order to create a custom switch, you must inherit a new class from the CustomSwitch hierarchy. Create a constructor that takes the ISwitchPackage interface, switch name, and the command prompt parameters, calling the base class. Then, override the Executing function and implement what you want. To monitor the switch execution, CustomSwitch has two events: BeforeExecuting and AfterExecuting.

Here, there is the code example for OpenWebSiteSwitch:

/// <span class="code-SummaryComment"><summary /></span>
/// Manages opening WebSite.
/// <span class="code-SummaryComment"></summary /></span>

internal class OpenWebSiteSwitch : CustomSwitch
    #region Constructors

    /// <span class="code-SummaryComment"><summary /></span>
    /// Default constructor.
    /// <span class="code-SummaryComment"></summary /></span>

    /// <span class="code-SummaryComment"><param name="package" />Package that launch command.</param /></span>
    /// <span class="code-SummaryComment"><param name="name" />Custom switch name.</param /></span>
    /// <span class="code-SummaryComment"><param name="parameters" />Custom switch parameters.</param /></span>

    public OpenWebSiteSwitch(ISwitchPackage package, string name, string[] parameters)
           : base(package, name, parameters)


    #region Protected Functions

    /// Executes the command.

    protected override void Executing()
        VsWebSite.VSWebPackage webPackage = 
          Package.DTE2.GetObject("WebPackage") as VsWebSite.VSWebPackage;

              VsWebSite.OpenWebsiteOptions.OpenWebsiteOption_None, false);
        catch (Exception ex)
            System.Windows.Forms.MessageBox.Show("Exception type: " + 
                   ex.GetType().ToString() + ". WebSite at path {0} cannot be loaded!", 


To fire events, the Execute method of the base class CustomSwitch is implemented as follows:

/// Checks parameters and executes the command.

virtual public void Execute()
    if (!CheckContext())
        throw new SwitchException(Resources.IllegalSwitchOrOption);

    if (BeforeExecuting != null)
        BeforeExecuting(this, null);


    if (AfterExecuting != null)
        AfterExecuting(this, null);

Some operations can be executed only when VS is completely opened, for example, the opening of a project or website. So, you can execute a switch only after the startup completed event.

At the end of the Initialize function of the package:

EnvDTE80.DTE2 dte2 = GetService(typeof(SDTE)) as EnvDTE80.DTE2;
dte2.Events.DTEEvents.OnStartupComplete += new 

In the event:

IVsAppCommandLine commandLine = GetService(typeof(SVsAppCommandLine)) as IVsAppCommandLine;
CustomSwitchEngine customSwitchEngine = new CustomSwitchEngine(this);
Collection<CustomSwitch> customSwitches = customSwitchEngine.GetCustomSwitches(commandLine);

foreach (CustomSwitch customSwitch in customSwitches)

IVsAppCommandLine is the interface that you require for VS and for using the command line arguments.

After implementation of your custom switch, you must update the Registry so Visual Studio recognizes your switches.


In HKLM/SOFTWARE/Microsoft/VisualStudio/8.0/AppCommandLine, you must add a new key with the same name of your switch and add the following values:

  • Arguments: number of arguments that the switch can have.
  • DemandLoad: 1 if the package is loaded when the switch is present in the command line.
  • HelpString: you can specify a help string for /? from the prompt. Example: myswitch /?.
  • Package: GUID of the package related to the switch.

To test your switch, you can load the code for the SimpleSwitchFramework (see prerequisites), open the project Properties and, in the Debug tab, add /AboutSwitch after the /rootsuffix Exp. When you press F5, another instance of VS will run, and after startup complete, a message box will appear.

Note: in the source folders, there are two .reg files to update the registry.


As you can see, there is a Configuration hierarchy that allows you to load all possible custom switches that VS can recognize. The CustomSwitchEngine has a property configuration so you can create your custom configuration and use it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Italy Italy
I am a biomedical engineer. I work in Genoa as software developer. I developed MFC ActiveX controls for industrial automation for 2 years and packages for Visual Studio 2005 for 1 year. Currently I'm working in .NET 3.5 in biomedical area.

Comments and Discussions

-- There are no messages in this forum --