プロジェクト項目のプロパティの保存

ソース ファイルの作成者など、プロジェクト項目に追加したプロパティを保存することもできます。 これを行うには、プロパティをプロジェクト ファイルに格納します。

プロジェクト ファイルでプロパティを保存するための最初の手順は、プロジェクトの階層を IVsHierarchy インターフェイスとして取得することです。 このインターフェイスは、オートメーションを使用するか、IVsMonitorSelection を使用して取得できます。 インターフェイスを取得したら、それを使用して、現在選択されているプロジェクト項目を特定できます。 プロジェクト項目 ID を取得したら、SetItemAttribute を使用してプロパティを追加できます。

次の手順では、VsPkg.cs プロパティ Author を値 Tom と共にプロジェクト ファイルに保存します。

DTE オブジェクトを使用してプロジェクト階層を取得するには

  1. 次のコードを VSPackage に追加します。

    EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetGlobalService(typeof(EnvDTE.DTE));
    EnvDTE.Project project = dte.Solution.Projects.Item(1);
    
    string uniqueName = project.UniqueName;
    IVsSolution solution = (IVsSolution)Package.GetGlobalService(typeof(SVsSolution));
    IVsHierarchy hierarchy;
    solution.GetProjectOfUniqueName(uniqueName, out hierarchy);
    

DTE オブジェクトを使用してプロジェクト項目のプロパティを保存するには

  1. 前の手順でメソッドに指定したコードに次のコードを追加します。

    IVsBuildPropertyStorage buildPropertyStorage =
        hierarchy as IVsBuildPropertyStorage;
    if (buildPropertyStorage != null)
    {
        uint itemId;
        string fullPath = (string)project.ProjectItems.Item(
            "VsPkg.cs").Properties.Item("FullPath").Value;
        hierarchy.ParseCanonicalName(fullPath, out itemId);
        buildPropertyStorage.SetItemAttribute(itemId, "Author", "Tom");
    }
    

IVsMonitorSelection を使用してプロジェクト階層を取得するには

  1. 次のコードを VSPackage に追加します。

    IVsHierarchy hierarchy = null;
    IntPtr hierarchyPtr = IntPtr.Zero;
    IntPtr selectionContainer = IntPtr.Zero;
    uint itemid;
    
    // Retrieve shell interface in order to get current selection
    IVsMonitorSelection monitorSelection =     Package.GetGlobalService(typeof(SVsShellMonitorSelection)) as     IVsMonitorSelection;
    if (monitorSelection == null)
        throw new InvalidOperationException();
    
    try
    {
        // Get the current project hierarchy, project item, and selection container for the current selection
        // If the selection spans multiple hierarchies, hierarchyPtr is Zero
        IVsMultiItemSelect multiItemSelect = null;
        ErrorHandler.ThrowOnFailure(
            monitorSelection.GetCurrentSelection(
                out hierarchyPtr, out itemid,
                out multiItemSelect, out selectionContainer));
    
        // We only care if there is only one node selected in the tree
        if (!(itemid == VSConstants.VSITEMID_NIL ||
            hierarchyPtr == IntPtr.Zero ||
            multiItemSelect != null ||
            itemid == VSConstants.VSITEMID_SELECTION))
        {
            hierarchy = Marshal.GetObjectForIUnknown(hierarchyPtr)
                as IVsHierarchy;
        }
    }
    finally
    {
        if (hierarchyPtr != IntPtr.Zero)
            Marshal.Release(hierarchyPtr);
        if (selectionContainer != IntPtr.Zero)
            Marshal.Release(selectionContainer);
    }
    

プロジェクト階層を指定して、選択したプロジェクト項目のプロパティを保存するには

  1. 前の手順でメソッドに指定したコードに次のコードを追加します。

    IVsBuildPropertyStorage buildPropertyStorage =
        hierarchy as IVsBuildPropertyStorage;
    if (buildPropertyStorage != null)
    {
        buildPropertyStorage.SetItemAttribute(itemId, "Author", "Tom");
    }
    

プロパティが保存されていることを確認するには

  1. Visual Studio を起動し、ソリューションを開くか作成します。

  2. ソリューション エクスプローラーでプロジェクト項目 VsPkg.cs を選択します。

  3. ブレークポイントを使用するか、または VSPackage が読み込まれていることと、SetItemAttribute が実行されることを確認します。

    Note

    UI コンテキスト SolutionExists_guid で VSPackage を自動読み込みできます。 詳細については、VSPackage を読み込むに関するページを参照してください。

  4. Visual Studio を閉じてから、メモ帳でプロジェクト ファイルを開きます。 次のように、値 Tom と共に <Author> タグが表示されます。

    <Compile Include="VsPkg.cs">
        <Author>Tom</Author>
    </Compile>