如何:访问特定类型的项目的配置属性

更新:2007 年 11 月

Visual Studio 常规自动化模型提供 Properties 集合,可使用它访问任何 Visual Studio 项目类型的 Properties 集合。此外,项目属性还使您可以控制安全设置,生成配置和调试配置。

若要手动设置和检查项目属性,请在 Visual Studio 集成开发环境 (IDE) 中打开一个项目。在“项目”菜单上单击“属性”。“属性”窗口有多个选项卡,并且每个窗格均列出了用于定义和控制项目行为的属性。自动化模型允许您以编程方式控制这些设置。具体来说,通过 CSharpProjectConfigurationProperties4VBProjectConfigurationProperties4JSharpProjectConfigurationProperties3ProjectConfigurationProperties3 中列出的属性,可以控制当前活动配置的“属性”页的“生成”(对 Visual Basic 项目为“编译”)和“调试属性”窗格中的项目属性。

也可以通过访问 ConfigurationManager 对象选择另一个(当前不活动的)配置。有关更多信息,请参见 如何:创建解决方案和项目生成配置

Visual C# 和 Visual J# 项目的配置属性分别在 CSharpProjectConfigurationProperties4JSharpProjectConfigurationProperties3 中定义。CodePageDisableLangXtnsJCPASecureScoping 属性只特定于 Visual J# 项目。ErrorReportLanguageVersion 属性只特定于 Visual C# 项目。CSharpProjectConfigurationProperties3JSharpProjectConfigurationProperties3 中的其他属性与 ProjectConfigurationProperties3 中的属性相同。

不能通过将 Property 对象直接强制转换为 CSharpProjectConfigurationProperties3JSharpProjectConfigurationProperties3ProjectConfigurationProperties3 对象来访问这些配置属性。但可以通过将配置项的名称作为字符串进行传递来访问这些属性,如下所示:

    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")

此代码访问 CSharpProjectConfigurationProperties3.EnableSQLServerDebuggingJSharpProjectConfigurationProperties3.EnableSQLServerDebuggingProjectConfigurationProperties3.EnableSQLServerDebugging 属性,具体取决于 proj 变量定义的是 Visual C#、Visual J# 还是 Visual Basic 项目。

实际上,在 CSharpProjectConfigurationProperties3JSharpProjectConfigurationProperties3ProjectConfigurationProperties3 中定义的配置属性是特定项目的可用配置属性的引用列表,这些特定项目可以作为项目配置属性项通过 Properties 集合访问。

下面的步骤详细描述如何以编程方式访问 Visual Studio 外接程序中的当前活动配置的配置属性。

ms228959.alert_note(zh-cn,VS.90).gif说明:

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您的当前设置或版本。这些过程是使用现用的常规开发设置开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置

访问特定类型项目的配置属性

  1. 使用 Visual C# 创建 Visual Studio 外接程序项目。

  2. 在“项目”菜单上单击“添加引用”,再单击“.NET”选项卡,然后选择“System.Windows.Forms”、“VSLangProj”、“VSLangProj2”和“VSLangProj80”,并单击“确定”。

  3. 将以下 using 语句添加到 Connect.cs 文件顶部。

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    using VSLangProj90;
    using System.Windows.Forms;
    
  4. 将以下函数调用添加到 OnConnection 函数。

    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    VSProjectConfigProperties(_applicationObject);
    
  5. 添加 VSProjectConfigProperties 方法,并使其紧跟在 OnConnection 方法的下方。

    public void VSProjectConfigProperties(DTE2 dte)
    {
        try
        {
            // Open a Visual C#, Visual J#, 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());
            }
            if (project.Kind == PrjKind2.prjKindVJSharpProject)
            {
                MessageBox.Show("The project is a Visual J# Project");
                prop = configProps.Item("CodePage");
                MessageBox.Show("The code page value is : " 
    + prop.Value.ToString());
                MessageBox.Show
    ("Setting the code page value to my code page");
                prop.Value = "my code page";
                MessageBox.Show("The code page value is now: " 
    + prop.Value.ToString());
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    VSProjectConfigProperties 方法获取并显示 PlatformTarget 属性值。该方法可设置或获取 WarningLevel 属性。如果该项目为 Visual C# 项目,则 VSProjectConfigProperties 方法将设置和获取 LanguageVersion 属性。如果该项目为 Visual J# 项目,则此方法将设置和获取 CodePage 属性。

  6. 通过单击“生成”菜单上的“生成解决方案”生成外接程序。

  7. 在 Visual Studio IDE 中打开一个 Visual C#、Visual J# 或 Visual Basic 项目。

  8. 在“工具”菜单上单击“外接程序管理器”,再从“外接程序管理器”对话框中选择您要的外接程序。单击“确定”以运行外接程序。

  9. 通过单击“项目”菜单上的“属性”,然后选择“属性”窗口中的“生成”选项卡,确认警告等级已被更改。

    “警告等级”字段反映出您以编程方式进行的更改。

  10. 若要验证 Visual C# 项目的语言版本设置,请在“属性”窗口中的“生成”窗格上单击“高级”。

    “高级生成设置”对话框的“语言版本”字段反映出外接程序所做的更改。

  11. 若要验证对 Visual J# 项目的代码页设置进行的更改,请在“属性”窗口中的“生成”窗格上单击“高级”。

    “高级生成设置”对话框的“代码页”字段反映出该属性的更改。

示例

下面的示例是一个简单的 Visual Studio 外接程序,演示如何通过使用 Visual Studio 中的自动化功能访问特定类型的项目的属性。

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
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#, Visual J#, 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());
        }
        if (project.Kind == PrjKind2.prjKindVJSharpProject)
        {
            MessageBox.Show("The project is a Visual J# Project");
            prop = configProps.Item("CodePage");
            MessageBox.Show("The code page value is : " 
+ prop.Value.ToString());
            MessageBox.Show
("Setting the code page value to my code page");
            prop.Value = "my code page";
            MessageBox.Show("The code page 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
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#, Visual J#, 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
        If project.Kind = PrjKind2.prjKindVJSharpProject Then
            MsgBox("The project is a Visual J# Project")
            prop = configProps.Item("CodePage")
            MsgBox("The code page value is : "  _
            & prop.Value.ToString())
            MsgBox("Setting the code page value to my code page")
            prop.Value = "my code page"
            MsgBox("The code page value is now: "  _
            & prop.Value.ToString())
        End If
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub

编译代码

若要编译此代码,请创建一个新的 Visual Studio 外接程序项目,然后用本示例中的代码替换 OnConnection 方法的代码。有关如何运行外接程序的信息,请参见 如何:使用外接程序管理器控制外接程序

请参见

概念

项目属性

其他资源

访问特定于项目类型的项目、项目项和配置属性