This documentation is archived and is not being maintained.

Walkthrough: Extending Managed VSPackages with Automation (C#)

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Visual Studio automation can provide a relatively lightweight way to extend your VSPackages; that is, add more functionality to them. This walkthrough illustrates how to use automation to create a managed VSPackage that manipulates the Visual Studio integrated development environment (IDE). By running the VSPackage Wizard, you create a sample managed VSPackage and then use automation methods in the resulting VSPackage to display Visual Studio properties in the Output window.

To create a managed VSPackage

  1. In Visual Studio, create a new Visual Studio Integration Package project.

    To do this, in the New Project dialog box expand Other Project Types (located in the Project Types pane), click Extensibility, and then click Visual Studio Integration Package in the Templates pane.

  2. In the Name box, enter Auto and then click OK.

    The Visual Studio Package Wizard runs. For more information about how to create a managed VSPackage, see How to: Create VSPackages (C#).

  3. On the Select a Programming Language page, set the language to Visual C#.

  4. Keep the default values in the Basic VSPackage Information page.

  5. On the Select VSPackage Options page, select the Menu Command check box.

  6. On the Command Options page, change the Command Name to Auto.

  7. Click the Finish button.

    The wizard generates a managed project named Auto.

  8. Build the solution and verify that it compiles without errors.

To call the Automation model

  1. In the Solution Explorer window, right-click the Auto project node and then click Add Reference.

  2. On the .NET tab of the Add Reference dialog box, double-click EnvDTE and EnvDTE80.

    This adds references to the EnvDTE and EnvDTE80 automation namespaces.

  3. In the VSPkg.cs file, add using EnvDTE; and using EnvDTE80; to the top of the file.

  4. Replace the body of the MenuItemCallback method with the following lines:

    private void MenuItemCallback(object sender, EventArgs e)
    {
        DTE dte;
        dte = (DTE) GetService(typeof(DTE));
        string myString = 
          "Name is " + dte.Name + "\rVersion is " + dte.Version;
    
        Windows windows = dte.Windows;
        Window window = 
            (Window) windows.Item(EnvDTE.Constants.vsWindowKindOutput);
        window.Visible = true;
    
        OutputWindow outputWindow = (OutputWindow) window.Object;
        OutputWindowPane outputWindowPane =         
          outputWindow.OutputWindowPanes.Add("Test");
        outputWindowPane.Activate();
        outputWindowPane.OutputString(myString);
    }
    

    This code calls GetService to obtain a DTE automation object that represents the Visual Studio IDE. The automation code in MenuItemCallback creates a new pane in the Output window named Test. The Visual Studio name and version is then written to the new Output pane.

  5. Build and start the Auto project in debug mode by pressing F5.

    This starts the Visual Studio experimental build (Visual Studio Exp).

    NoteNote

    Both instances of Visual Studio are open at this point. That is, the instance of Visual Studio where your code is located and the experimental build instance.

  6. In Visual Studio Exp, on the Tools menu, click Auto.

    A new pane named Test opens in the Output window and displays the following:

    Name is Microsoft Visual Studio
    Version is x.xx
    

    Where x.xx is the latest Visual Studio version number.

    For more information about automation samples, see Automation S a mples for Visual Studio.

Show: