Partager via


Walkthrough: Retrieving Project Properties

This walkthrough shows how to create a managed VSPackage that displays project properties in a tool window. You can create a sample managed VSPackage by using the Visual Studio Package project template. Use automation with the resulting VSPackage to display project properties in a tool window. Follow these steps to create a managed VSPackage that has a tool window and then extend it by using automation:

  • Create the managed VSPackage.

  • Call the automation model to display project properties in a tool window.

Prerequisites

To complete this walkthrough, you must install the Visual Studio 2010 SDK.

Notes

For more information about the Visual Studio SDK, see Visual Studio Integration SDK. To find out how to download the Visual Studio SDK, see Visual Studio Extensibility Developer Center on the MSDN Web site.

Locations for the Visual Studio Package Project Template

The Visual Studio Package project template can be found in three different locations in the New Project dialog:

  1. Under Visual Basic Extensibility. The default language of the project is Visual Basic.

  2. Under C# Extensibility. The default language of the project is C#.

  3. Under Other Project Types Extensibility. The default language of the project is C++.

To create a managed VSPackage

  1. Create a VSPackage project named ProjectProps by using the Visual Studio Package project template.

    For more information about how to create a managed VSPackage, see Walkthrough: Creating a Menu Command By Using the Visual Studio Package Template.

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

  2. On the Basic VSPackage Information page, accept the default values.

  3. On the Select VSPackage Options page, select Menu Command and Tool Window.

  4. On the Command Options page, type My Toolbox.

  5. On the Tool Window Options page, type ProjectProps Tool Window.

  6. Click Finish.

    The template generates a managed project named ProjectProps.

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

To call the automation model to display project properties in a tool window

  1. In Solution Explorer, right-click the ProjectProps node and then click Add Reference.

  2. In the Add Reference dialog box, on the .NET tab, double-click EnvDTE and then click OK.

    This establishes a reference to the EnvDTE namespace.

  3. Add the following line at the top of the VSPkg.cs or VSPkg.vb file.

    Imports EnvDTE
    
    using EnvDTE;
    
  4. Add the following line at the top of the body of the ProjectProps class.

    Public Shared dte As EnvDTE.DTE
    
    public static EnvDTE.DTE dte;
    
  5. Add the following line at the top of the body of the ShowToolWindow method.

    dte = CType(GetService(GetType(DTE)), DTE)
    
    dte = (DTE)GetService(typeof(DTE));
    

    This code uses GetService method to get a DTE automation object that represents the Visual Studio environment.

  6. Open MyControl.cs or MyControl.vb in the Visual Studio designer and remove the Click Me! button that was inserted by the Visual Studio Package project template.

  7. Open MyControl.cs or MyControl.vb in the source code editor. Add the following line at the top of the body of the MyControl class.

    Private treeView1 As TreeView
    
    private TreeView treeView1;
    
  8. Replace the body of the MyControl constructor by using the following lines.

    Public Sub New()
        ' This call is required by the Windows.Forms Form Designer.
        InitializeComponent()
    
        ' 
        ' treeView1
        ' 
        Me.treeView1 = New System.Windows.Forms.TreeView()
        Me.SuspendLayout()
        Me.treeView1.Location = New System.Drawing.Point(20, 20)
        Dim project As EnvDTE.Project
        project = ProjectProps.dte.Solution.Projects.Item(1)
        Dim nodeNum As Integer = 0
        Me.treeView1.Name = "ProjectPropsTree"
        Me.treeView1.Nodes.Add(New System.Windows.Forms.TreeNode(project.Name & " Properties"))
        For Each [property] As EnvDTE.Property In project.Properties
            Dim node As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("Node" & nodeNum)
            node.Name = "Node" & nodeNum
            nodeNum = nodeNum + 1
            node.Text = [property].Name
            treeView1.Nodes(0).Nodes.Add(node)
        Next [property]
        Me.treeView1.Size = New System.Drawing.Size(250, 500)
        Me.treeView1.TabIndex = 1
        Me.Controls.Add(Me.treeView1)
        Me.ResumeLayout(False)
    End Sub
    
    public MyControl()
    {
        // This call is required by the Windows.Forms Form Designer.
        InitializeComponent();
    
        // 
        // treeView1
        // 
        this.treeView1 = new System.Windows.Forms.TreeView();
        this.SuspendLayout();
        this.treeView1.Location = new System.Drawing.Point(20, 20);
        EnvDTE.Project project;
        project = ProjectProps.dte.Solution.Projects.Item(1);
        int nodeNum = 0;
        this.treeView1.Name = "ProjectPropsTree";
        this.treeView1.Nodes.Add(new System.Windows.Forms.TreeNode(project.Name + 
                                                                   " Properties"));
        foreach (EnvDTE.Property property in project.Properties)
        {
            System.Windows.Forms.TreeNode node = 
                           new System.Windows.Forms.TreeNode("Node" + nodeNum);
            node.Name = "Node" + nodeNum++;
            node.Text = property.Name;
            treeView1.Nodes[0].Nodes.Add(node);
        }
        this.treeView1.Size = new System.Drawing.Size(250, 500);
        this.treeView1.TabIndex = 1;
        this.Controls.Add(this.treeView1);
        this.ResumeLayout(false);
    }
    

    This code uses DTE automation objects to retrieve the project properties and to dynamically populate the tree control in the tool window by using the project properties name values.

  9. Build the ProjectProps project.

  10. Run the ProjectProps project by pressing F5, or CTRL+F5 to run the Visual Studio experimental build.

    Notes

    Two versions of Visual Studio are now open.

  11. In Visual Studio Exp, create or open any project.

  12. On the View menu, point to Other Windows and click ProjectProps Tool Window.

    You should see the tree control in the tool window together with the project properties name values.

See Also

Tasks

How to: Create and Control Tool Windows

Concepts

Visual Studio SDK and Automation

Automation Model