Megosztás a következőn keresztül:


How to: Access Configuration Properties for Specific Types of Projects

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 build configuration and the debugging configuration.

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 lets you control these settings programmatically. Specifically, the properties listed in CSharpProjectConfigurationProperties4, VBProjectConfigurationProperties4, and ProjectConfigurationProperties3 enable you to control the project properties found on the Build (Compile for Visual Basic projects) and Debug Properties panes of the Properties page of the currently active configuration.

You can also choose a different, not currently active, configuration by accessing the ConfigurationManager object. For more information, see How to: Create Solution and Project Build Configurations.

Configuration properties for Visual C# projects are defined in CSharpProjectConfigurationProperties4. The ErrorReport and LanguageVersion properties are specific to Visual C# projects only. The rest of the properties in CSharpProjectConfigurationProperties3 are the same as the properties in ProjectConfigurationProperties3.

These configuration properties cannot be accessed by directly casting a Property object to a CSharpProjectConfigurationProperties3 or ProjectConfigurationProperties3 object. Instead, you can access these properties by passing the name of the configuration item as a string as shown below:

    EnvDTE.Project proj;
    EnvDTE.Configuration config;
    EnvDTE.Properties configProps;
    EnvDTE.Property prop;
    proj = DTE.Solution.Projects.Item(1);
    config = proj.ConfigurationManager.ActiveConfiguration;
    configProps = config.Properties;
    prop = configProps.Item("EnableSQLServerDebugging")

This code accesses either the CSharpProjectConfigurationProperties3.EnableSQLServerDebugging or ProjectConfigurationProperties3.EnableSQLServerDebugging property depending on whether the proj variable defines a Visual C# or Visual Basic project.

In effect, the configuration properties defined in CSharpProjectConfigurationProperties3 or ProjectConfigurationProperties3 are a reference list of available configuration properties for specific projects that can be accessed as project configuration property items through Properties collection.

The steps below detail how to programmatically access the configuration properties of the currently active configuration in a Visual Studio add-in.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

To access configuration properties for a specific type of project

  1. Create a Visual Studio add-in project by using Visual C# and selecting the option to load the add-in when Visual Studio starts.

  2. On the Project menu, click Add Reference, click the .NET tab, select System.Windows.Forms, VSLangProj, VSLangProj2, and VSLangProj80, 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 the following function call to the OnConnection function.

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

    public void VSProjectConfigProperties(DTE2 dte)
    {
        try
        {
            // Open a Visual C# or Visual Basic project
            // before running this add-in.
            Project project;
            Configuration config;
            Properties configProps;
            Property prop;
            project = _applicationObject.Solution.Projects.Item(1);
            config = project.ConfigurationManager.ActiveConfiguration;
            configProps = config.Properties;
            prop = configProps.Item("PlatformTarget");
            MessageBox.Show("The platform target for this project is: "
     + prop.Value.ToString());
            prop = configProps.Item("WarningLevel");
            MessageBox.Show
    ("The warning level for this project is set to: " 
    + prop.Value.ToString());
            MessageBox.Show("Changing the warning level to 3...");
            prop.Value = "3";
            MessageBox.Show
    ("The warning level for this project is now set to: " 
    + prop.Value.ToString());
            if (project.Kind == PrjKind.prjKindCSharpProject)
            {
                MessageBox.Show("The project is a Visual C# Project");
                prop = configProps.Item("LanguageVersion");
                MessageBox.Show("The language version value is : " 
    + prop.Value.ToString());
                MessageBox.Show("Setting the language version to 
    ISO-1");
                prop.Value = "ISO-1";
                MessageBox.Show("The language version value is now: " 
    + prop.Value.ToString());
            }
    
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    The VSProjectConfigProperties method gets and displays the PlatformTarget property value. It both sets and gets the WarningLevel property. If the project is a Visual C# project, the VSProjectConfigProperties method sets and gets the LanguageVersion property.

  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 warning level has changed by clicking Properties on the Project menu, and then selecting the Build tab in the Properties window.

    The Warning level field reflects the change you made programmatically.

  10. To validate the language version setting for a Visual C# project, on the Build pane of the Properties window, click on Advanced.

    The Language Version field of the Advanced Build Settings dialog box reflects the change your add-in made.

Example

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

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;
public void OnConnection(object application, 
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    VSProjectConfigProperties(_applicationObject);
}
public void VSProjectConfigProperties(DTE2 dte)
{
    try
    {
        // Open a Visual C# or Visual Basic project
        // before running this add-in.
        Project project;
        Configuration config;
        Properties configProps;
        Property prop;
        project = _applicationObject.Solution.Projects.Item(1);
        config = project.ConfigurationManager.ActiveConfiguration;
        configProps = config.Properties;
        prop = configProps.Item("PlatformTarget");
        MessageBox.Show("The platform target for this project is: 
" + prop.Value.ToString());
        prop = configProps.Item("WarningLevel");
        MessageBox.Show
("The warning level for this project is set to: " 
+ prop.Value.ToString());
        MessageBox.Show("Changing the warning level to 3...");
        prop.Value = "3";
        MessageBox.Show
("The warning level for this project is now set to: " 
+ prop.Value.ToString());
        if (project.Kind == PrjKind.prjKindCSharpProject)
        {
            MessageBox.Show("The project is a Visual C# Project");
            prop = configProps.Item("LanguageVersion");
            MessageBox.Show("The language version value is : " 
+ prop.Value.ToString());
            MessageBox.Show("Setting the language version to ISO-1");
            prop.Value = "ISO-1";
            MessageBox.Show("The language version 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 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)
    VSProjectConfigProperties(_applicationObject)
End Sub
Sub VSProjectConfigProperties(ByVal dte As DTE2)
    ' Open a Visual C# or Visual Basic project
    ' before running this add-in.
    Try
        Dim project As Project
        Dim config As Configuration
        Dim configProps As Properties
        Dim prop As [Property]
        project = _applicationObject.Solution.Projects.Item(1)
        config = project.ConfigurationManager.ActiveConfiguration
        configProps = config.Properties
        prop = configProps.Item("PlatformTarget")
        MsgBox("The platform target for this project is: "  _
        & prop.Value.ToString())
        prop = configProps.Item("WarningLevel")
        MsgBox("The warning level for this project is set to: "  _
        & prop.Value.ToString())
        MsgBox("Changing the warning level to 3...")
        prop.Value = "3"
        MsgBox("The warning level for this project is now set to: " _
        & prop.Value.ToString())
        If project.Kind = PrjKind.prjKindCSharpProject Then
            MsgBox("The project is a Visual C# Project")
            prop = configProps.Item("LanguageVersion")
            MsgBox("The language version value is : "  _
            & prop.Value.ToString())
            MsgBox("Setting the language version to ISO-1")
            prop.Value = "ISO-1"
            MsgBox("The language version 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