次の方法で共有


方法 : 特定の種類のプロジェクトの構成プロパティにアクセスする

Visual Studio の一般的なオートメーション モデルには、Properties コレクションが用意されています。このコレクションを使用して、Visual Studio のすべての種類のプロジェクトの Properties コレクションにアクセスできます。 特に、プロジェクト プロパティを使用すると、セキュリティ設定、ビルド構成、およびデバッグ構成を制御できるようになります。

プロジェクト プロパティを手動で設定および調査するには、Visual Studio 統合開発環境 (IDE: Integrated Development Environment) でプロジェクトを開きます。 [プロジェクト] メニューの [プロパティ] をクリックします。 [プロパティ] ウィンドウには複数のタブがあり、各ペインには、プロジェクトの動作を定義および制御する際に使用するプロパティの一覧が表示されます。 オートメーション モデルを使用すると、これらの設定をプログラムで制御できます。 具体的には、CSharpProjectConfigurationProperties4VBProjectConfigurationProperties4、および ProjectConfigurationProperties3 に示されているプロパティを使用すると、現在アクティブな構成の [プロパティ] ページの [ビルド] (Visual Basic プロジェクトの場合は [コンパイル]) ウィンドウ ペインおよび [デバッグ] プロパティ ウィンドウ ペインにあるプロジェクトのプロパティを制御できます。

また、ConfigurationManager オブジェクトにアクセスして、現在アクティブになっていない別の構成を選択することもできます。 詳細については、「方法 : ソリューションとプロジェクトのビルド構成を作成する」を参照してください。

Visual C# プロジェクトの構成プロパティは、CSharpProjectConfigurationProperties4 で定義されています。 ErrorReport プロパティおよび LanguageVersion プロパティは、Visual C# プロジェクトのみに固有のプロパティです。 CSharpProjectConfigurationProperties3 内の残りのプロパティは、ProjectConfigurationProperties3 内のプロパティと同じです。

これらの構成プロパティは、Property オブジェクトを CSharpProjectConfigurationProperties3 オブジェクトまたは ProjectConfigurationProperties3 オブジェクトに直接キャストしてもアクセスできません。 その代わり、次に示すように、構成項目の名前を文字列として渡すことにより、これらのプロパティにアクセスできます。

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

このコードは、proj 変数で Visual C# プロジェクトまたは Visual Basic プロジェクトのどちらが定義されているかに応じて、CSharpProjectConfigurationProperties3.EnableSQLServerDebugging プロパティまたは ProjectConfigurationProperties3.EnableSQLServerDebugging プロパティにアクセスします。

実際には、CSharpProjectConfigurationProperties3 または ProjectConfigurationProperties3 で定義された構成プロパティは、Properties コレクションを使用してプロジェクトの構成プロパティ項目としてアクセスできる特定のプロジェクトの利用可能な構成プロパティの参照一覧です。

次の手順では、Visual Studio アドインで現在アクティブな構成の構成プロパティにプログラムでアクセスする方法を説明します。

注意

実際に画面に表示されるダイアログ ボックスとメニュー コマンドは、アクティブな設定またはエディションによっては、ヘルプの説明と異なる場合があります。 ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。 設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。 詳細については、「設定の操作」を参照してください。

特定の種類のプロジェクトの構成プロパティにアクセスするには

  1. Visual C# を使用し、Visual Studio の起動時にアドインを読み込むオプションを選択して、Visual Studio アドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックし、[.NET] タブを選択します。[System.Windows.Forms][VSLangProj][VSLangProj2]、および [VSLangProj80] を選択し、[OK] をクリックします。

  3. 次の using ステートメントを Connect.cs ファイルの先頭に追加します。

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    using VSLangProj90;
    using VSLangProj100;
    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# 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);
        }
    }
    

    VSProjectConfigProperties メソッドは、PlatformTarget プロパティ値を取得して表示します。 このメソッドは、WarningLevel プロパティの設定および取得の両方を行います。 プロジェクトが Visual C# プロジェクトの場合、VSProjectConfigProperties メソッドは、LanguageVersion プロパティの設定および取得を行います。

  6. [ビルド] メニューの [ソリューションのビルド] をクリックし、アドインをビルドします。

  7. Visual Studio IDE で、Visual C# プロジェクトまたは Visual Basic プロジェクトを開きます。

  8. [ツール] メニューの [アドイン マネージャー] をクリックし、[アドイン マネージャー] ダイアログ ボックスからアドインを選択します。 [OK] をクリックしてアドインを実行します。

  9. [プロジェクト] メニューの [プロパティ] をクリックし、[プロパティ] ウィンドウの [ビルド] タブを選択して、警告レベルが変更されたことを確認します。

    [警告レベル] フィールドには、プログラムで加えた変更が反映されます。

  10. Visual C# プロジェクトの言語バージョンの設定を確認するには、[プロパティ] ウィンドウの [ビルド] ペインで、[詳細設定] をクリックします。

    [ビルドの詳細設定] ダイアログ ボックスの [言語バージョン] フィールドには、アドインによる変更が反映されます。

使用例

基本的な Visual Studio アドインの例を次に示します。この例では、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

コードのコンパイル

このコードをコンパイルするには、新しい Visual Studio アドイン プロジェクトを作成し、OnConnection メソッドのコードをこの例のコードで置き換えます。 アドインの実行方法については、「方法: アドイン マネージャーを使用してアドインを制御する」を参照してください。

参照

概念

プロジェクト プロパティ

その他の技術情報

プロジェクトの種類に固有のプロジェクト、プロジェクト項目、および構成プロパティへのアクセス