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 follow this walkthrough, you must install the Visual Studio 2013 SDK. For more information, see Visual Studio Software Development Kit (SDK).
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:
Under Visual Basic Extensibility. The default language of the project is Visual Basic.
Under C# Extensibility. The default language of the project is C#.
Under Other Project Types Extensibility. The default language of the project is C++.
To create a managed VSPackage
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.
On the Basic VSPackage Information page, accept the default values.
On the Select VSPackage Options page, select Menu Command and Tool Window.
On the Command Options page, type My Toolbox.
On the Tool Window Options page, type ProjectProps Tool Window.
Click Finish.
The template generates a managed project named ProjectProps.
Build the solution and verify that it compiles without errors.
To call the automation model to display project properties in a tool window
In Solution Explorer, right-click the ProjectProps node and then click Add, Reference.
In the Reference dialog box, on the .NET tab, double-click EnvDTE and then click OK.
This establishes a reference to the EnvDTE namespace.
Add the following line at the top of the VSPkg.cs or VSPkg.vb file.
Imports EnvDTE
using EnvDTE;
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;
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.
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.
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;
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.
Build the ProjectProps project.
Run the ProjectProps project by pressing F5, or CTRL+F5 to run the Visual Studio experimental build.
Note
Two versions of Visual Studio are now open.
In Visual Studio Exp, create or open any project.
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