How to: Access Properties of Specific Project Types

The Visual Studio general automation model provides the Properties collection that can be used to access the Properties collections of any Visual Studio project type. Among other things, project properties enable you to control security settings, the assembly name, and so on.

To manually set and examine project properties, open a project in the Visual Studio integrated development environment (IDE). On the Project menu click Properties. The Properties window has multiple tabs and each pane lists properties that are used to define and control the behavior of projects. The automation model allows you to control these settings programmatically. Specifically, the properties in ProjectProperties3 enable you to control the project properties found on the Application, Resources, Settings, Reference Paths and Signing window panes of the Properties page for Visual C# projects. The properties defined in VBProjectProperties3 enable you to control the properties settings of Visual Basic projects, found on the Application, Resources, Settings, References and Signing window panes of the Properties page.

Properties for Visual C# projects are defined in ProjectProperties3. Properties for Visual Basic projects are defined in VBProjectProperties3. The MyApplication and MyType properties are specific to Visual Basic projects only. The rest of the properties in VBProjectProperties3 are the same as the properties in ProjectProperties3.

These properties cannot be accessed by directly casting a Properties object to a ProjectProperties3 or a VBProjectProperties3 object. Instead, these properties must be accessed through the Properties collection by supplying the name of the property for the specific type of project as a string to specify the Property. For example, the code, EnvDTE.Property prop = EnvDTE.Properties.Item("ApplicationIcon"); enables you to access the ApplicationIcon property.

In effect, the properties defined in ProjectProperties3 and VBProjectProperties3 are a reference list of available properties for specific projects that can be accessed as project property items.

The steps below detail how to access these properties programmatically in a Visual Studio add-in.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To access properties for a specific type of project

  1. Create a Visual Studio Add-in project by using Visual C#.

  2. On the Project menu, click Add Reference, click the .NET tab, select VSLangProj, VSLangProj2, and VSLangProj80, and Sytem.Windows.Forms and click OK.

  3. Add the following using-statements to the top of the Connect.cs file.

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    using VSLangProj90;
    using VSLangProj100;
    using System.Windows.Forms;
    
  4. Add a call to a new method called VSProjectProperies to the OnConnection method after line that sets the _addInstance method as shown:.

    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    VSProjectProperties(_applicationObject);
    
  5. Add the VSProjectProperties method right below the OnConnection method.

    public void VSProjectProperties(DTE2 dte)
    {
        try
        {
            // Open a Visual C# or Visual Basic project
            // before running this add-in.
            Project project;
            project = _applicationObject.Solution.Projects.Item(1);
            Property prop;
            prop = project.Properties.Item("AssemblyName");
            MessageBox.Show("The assembly name is: " 
    + prop.Value .ToString());
            prop.Value = "MyTestAssembly";
            MessageBox.Show("The assembly name is now: " 
    + prop.Value.ToString());
            // If the project is a Visual Basic project, set
            // the MyApplication property.
            if (project.Kind == "{F184B08F-C81C-45F6-A5F6-5ABD9991F28F}")
    
            {
                MessageBox.Show("The project is a Visual 
    Basic Project");
                prop = project.Properties.Item("MyType");
                MessageBox.Show("The MyType value is: " 
    + prop.Value.ToString());
                prop.Value = "Class Library";
                MessageBox.Show("The MyType value is now: " 
    + prop.Value.ToString());
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    The VSProjectProperties method both sets and gets the AssemblyName property by passing it in as a Property item string to the Properties collection. If the project is a Visual Basic project, the VSProjectProperties method also sets and gets the MyType property.

    The example section lists the complete code.

  6. Build the add-in by clicking Build Solution on the Build Menu.

  7. Open a Visual C# or Visual Basic project in the Visual Studio IDE.

  8. On the Tools menu, click Add-in Manager, and select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.

  9. Validate that the assembly name has changed by clicking Properties on the Project menu, and then selecting the Application tab in the Properties window.

    The Assembly name field reflects the change you made programmatically.

Example

The following example is a basic Visual Studio add-in that demonstrates how to access properties that are specific to project types by using automation in Visual Studio.

using System;
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using EnvDTE90;
using EnvDTE90a;
using EnvDTE100;
using System.Windows.Forms;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
using VSLangProj90;
using VSLangProj100;
namespace myAddin
    public void OnConnection(object application, 
ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        VSProjectProperties(_applicationObject);
    }
    public void VSProjectProperties(DTE2 dte)
    {
        try
        {
            // Open a Visual C# or Visual Basic project
            // before running this add-in.
            Project project;
            project = _applicationObject.Solution.Projects.Item(1);
            Property prop;
            prop = project.Properties.Item("AssemblyName");
            MessageBox.Show("The assembly name is: " 
+ prop.Value .ToString());
            prop.Value = "MyTestAssembly";
            MessageBox.Show("The assembly name is now: " 
+ prop.Value.ToString());
            // If the project is a Visual Basic project, set
            // the MyApplication property.
            if (project.Kind == PrjKind.prjKindVBProject)
            {
                MessageBox.Show
("The project is a Visual Basic Project");
                prop = project.Properties.Item("MyType");
                MessageBox.Show("The MyType value is: " 
+ prop.Value.ToString());
                prop.Value = "Class Library";
                MessageBox.Show("The MyType value is now: " 
+ prop.Value.ToString());
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports VSLangProj
Imports VSLangProj2
Imports VSLangProj80
Imports VSLangProj90
Imports VSLangProj100

    Public Sub OnConnection(ByVal application As Object, _
 ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
 ByRef custom As Array) Implements IDTExtensibility2.OnConnection
        _applicationObject = CType(application, DTE2)
        _addInInstance = CType(addInInst, AddIn)
        VSProjectProperties(_applicationObject)
    End Sub
    Sub VSProjectProperties(ByVal dte As DTE2)
        ' Open a Visual C# or Visual Basic project
        ' before running this add-in.
        Try
            Dim project As Project
            project = _applicationObject.Solution.Projects.Item(1)
            Dim prop As [Property]
            prop = project.Properties.Item("AssemblyName")
            MsgBox("The assembly name is: "  _
            & prop.Value.ToString())
            prop.Value = "MyTestAssembly"
            MsgBox("The assembly name is now: "  _
            & prop.Value.ToString())
            ' If the project is a Visual Basic project, set
            ' the MyApplication property.
            If project.Kind = PrjKind.prjKindVBProject Then
                MsgBox("The project is a Visual Basic Project")
                prop = project.Properties.Item("MyType")
                MsgBox("The MyType value is: "  _
                & prop.Value.ToString())
                prop.Value = "Class Library"
                MsgBox("The MyType value is now: "  _
                & prop.Value.ToString())
            End If
        Catch ex As System.Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

Compiling the Code

To compile this code, create a new Visual Studio Add-in project and replace the code of the OnConnection method with the code in the example. For information about how to run an add-in, see How to: Control Add-Ins By Using the Add-In Manager.

See Also

Other Resources

Project Properties

Accessing Project Type Specific Project, Project Item, and Configuration Properties