次の方法で共有


方法 : 特定の種類のプロジェクトのフォルダーのプロパティにアクセスする

Visual Studio 統合開発環境 (IDE: Integrated Development Environment) でプロジェクトを開き、ソリューション エクスプローラーでフォルダーを右クリックすると、フォルダーのプロパティを手動で設定および調査できます。 ショートカット メニューの [プロパティ] をクリックして [プロパティ] ダイアログ ボックスを表示します。

VSLangProj80 名前空間には、Visual C# プロジェクトまたは Visual Basic プロジェクトのフォルダー プロパティにプログラムでアクセスする方法が用意されています。 具体的には、FolderProperties2 では、フォルダー情報を制御したり、フォルダー情報にアクセスしたりするための豊富なプロパティ セットが定義されています。 FolderProperties2 で定義されているプロパティの多くには、[プロパティ] ウィンドウから手動でアクセスできません。

特定の FolderProperties2 プロパティにアクセスするには、次のコード例に示すように、そのプロパティの名前を文字列として EnvDTE.Property.Properties.Item(object index) に渡す必要があります。

Project project;
ProjectItem folder;
Properties folderProps;
Property prop;
project = _applicationObject.Solution.Projects.Item(1);
folder = project.ProjectItems.AddFolder("MyFolder"
,Constants.vsProjectItemKindPhysicalFolder);
folderProps = folder.Properties;
prop = folderProps.Item("FullPath");

このコードは、Visual C# プロジェクトまたは Visual Basic プロジェクト内のフォルダーの FullPath プロパティにアクセスします。

実際には、FolderProperties2 で定義されたプロパティは、Visual C# または Visual Basic のプロジェクトのプロパティ項目としてアクセスできるフォルダーの利用可能なプロパティの参照一覧です。

次の手順では、Visual Studio アドインでこれらのプロパティにプログラムでアクセスする方法を説明します。

注意

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

特定の種類のプロジェクトのフォルダー プロパティにアクセスするには

  1. Visual C# を使用して、Visual Studio アドイン プロジェクトを作成します。

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

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

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    
  4. 次のメソッド呼び出しを OnConnection メソッドに追加します。

    public void OnConnection(object application, 
    ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        VSProjectFolderProps2(_applicationObject);
    }
    
  5. VSProjectFolderProps2 メソッドを OnConnection メソッドの直後に追加します。

    public void VSProjectFolderProps2(DTE2 dte)
    {
        try
        {
            // Open a Visual C# or Visual Basic project
            // before running this add-in.
            Project project;
            ProjectItem folder;
            Properties folderProps;
            Property prop;
            project = _applicationObject.Solution.Projects.Item(1);
            // Add a new folder to the project.
            MessageBox.Show("Adding a new folder to the project.");
            folder =
     project.ProjectItems.AddFolder("MyFolder",
    Constants.vsProjectItemKindPhysicalFolder);
            folderProps = folder.Properties;
            prop = folderProps.Item("FullPath");
            MessageBox.Show("The full path of the new folder is:" 
    + "\n" + prop.Value.ToString());
            prop = folderProps.Item("FileName");
            MessageBox.Show("The file name of the new folder is:" 
    + "\n" + prop.Value.ToString());
            prop = folderProps.Item("URL");
            MessageBox.Show("The new folder has the following URL:" 
    + "\n" + prop.Value.ToString());
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    以下の使用例では、完全なコードを示します。

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

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

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

    FullPathFileName、および URL のフォルダー プロパティは、メッセージ ボックスに表示されます。

使用例

基本的な 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;
    VSProjectFolderProps2(_applicationObject);
}
public void VSProjectFolderProps2(DTE2 dte)
{
    try
    {
        // Open a Visual C# or Visual Basic project
        // before running this add-in.
        Project project;
        ProjectItem folder;
        Properties folderProps;
        Property prop;
        project = _applicationObject.Solution.Projects.Item(1);
        // Add a new folder to the project.
        MessageBox.Show("Adding a new folder to the project.");
        folder =
 project.ProjectItems.AddFolder("MyFolder"
,Constants.vsProjectItemKindPhysicalFolder);
        folderProps = folder.Properties;
        prop = folderProps.Item("FullPath");
        MessageBox.Show("The full path of the new folder is:" + "\n" 
+ prop.Value.ToString());
        prop = folderProps.Item("FileName");
        MessageBox.Show("The file name of the new folder is:" + "\n" 
+ prop.Value.ToString());
        prop = folderProps.Item("URL");
        MessageBox.Show("The new folder has the following URL:" 
+ "\n" + 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# or Visual Basic project
    ' before running this add-in.
    Try
        Dim project As Project
        Dim folder As ProjectItem
        Dim folderProps As Properties
        Dim prop As [Property]
        project = _applicationObject.Solution.Projects.Item(1)
        ' Add a new folder to the project.
        MsgBox("Adding a new folder to the project...")
        folder = project.ProjectItems.AddFolder("MyFolder" _
        , Constants.vsProjectItemKindPhysicalFolder)
        folderProps = folder.Properties
        prop = folderProps.Item("FullPath")
        MsgBox("The full path of the new folder is:" & vbCr _
        & prop.Value.ToString())
        prop = folderProps.Item("FileName")
        MsgBox("The file name of the new folder is:" & vbCr _
        & prop.Value.ToString())
        prop = folderProps.Item("URL")
        MsgBox("The new folder has the following URL:" & vbCr  _
        & prop.Value.ToString())
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub

コードのコンパイル

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

参照

概念

プロジェクト プロパティ

その他の技術情報

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