共用方式為


How to: 將保存的專案項目屬性

若要在保存的屬性,您將加入至專案項目,例如原始程式檔的作者。 您可以儲存專案檔中的屬性。

保存屬性,以在專案檔中的第一步是取得專案的階層架構IVsHierarchy介面。 您可以取得這個介面,使用自動化,或使用IVsMonitorSelection。 一旦您取得的介面,您可用它來判斷哪一個專案項目目前未選取。 一旦您有的專案項目識別碼,您可以使用SetItemAttribute以加入屬性。

下列程序,您可以保存 VsPkg.cs 屬性作者 值 唐美專案檔中。

若要使用自動化來取得專案階層架構

  • 將下列程式碼加入至您的 VSPackage:

    Dim dte As EnvDTE.DTE = CType(Package.GetGlobalService(GetType(EnvDTE.DTE)), EnvDTE.DTE)
    Dim project As EnvDTE.Project = dte.Solution.Projects.Item(1)
    
    Dim uniqueName As String = project.UniqueName
    Dim solution As IVsSolution = CType(Package.GetGlobalService(GetType(SVsSolution)), IVsSolution)
    Dim hierarchy As IVsHierarchy
    solution.GetProjectOfUniqueName(uniqueName, hierarchy)
    
    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);
    

使用自動化來保存專案項目屬性

  • 在先前程序中的方法中指定的程式碼中加入下列程式碼:

    Dim buildPropertyStorage As IVsBuildPropertyStorage = CType(hierarchy, IVsBuildPropertyStorage)
    If Not buildPropertyStorage Is Nothing Then 
        Dim itemId As UInteger 
        Dim fullPath As String = CType(project.ProjectItems.Item("VsPkg.vb").Properties.Item("FullPath").Value, String)
        hierarchy.ParseCanonicalName(fullPath, itemId)
        buildPropertyStorage.SetItemAttribute(itemId, "Author", "Tom")
    End If
    
    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:

    Dim hierarchy As IVsHierarchy = Nothing 
    Dim hierarchyPtr As IntPtr = IntPtr.Zero
    Dim selectionContainer As IntPtr = IntPtr.Zero
    Dim itemId As UInteger 
    
    ' Retrieve shell interface in order to get current selection 
    Dim monitorSelection As IVsMonitorSelection = CType(Package.GetGlobalService(GetType(SVsShellMonitorSelection)), IVsMonitorSelection)
    If monitorSelection Is Nothing Then 
        Throw New InvalidOperationException
    End If 
    
    Try 
        ' Get the current project hierarchy, project item, and selection container for the current selection 
        ' If the selection spans multiple hierarchies, hierarchyPtr is Zero 
        Dim multiItemSelect As IVsMultiItemSelect = Nothing
        ErrorHandler.ThrowOnFailure(monitorSelection.GetCurrentSelection(
                                    hierarchyPtr, itemId, multiItemSelect, selectionContainer))
    
        ' We only care if there is only one node selected in the tree 
        If Not (itemId = VSConstants.VSITEMID.Nil _
                Or hierarchyPtr = IntPtr.Zero _
                Or (Not multiItemSelect Is Nothing) _
                Or itemId = VSConstants.VSITEMID.Selection) Then
            hierarchy = CType(Marshal.GetObjectForIUnknown(hierarchyPtr), IVsHierarchy)
        End If 
    Finally 
        If hierarchyPtr <> IntPtr.Zero Then
            Marshal.Release(hierarchyPtr)
        End If 
        If selectionContainer <> IntPtr.Zero Then
            Marshal.Release(selectionContainer)
        End If 
    End Try
    
    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 hierachies, 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);
    }
    

將 [選取的專案項目] 屬性中,指定專案階層架構

  • 在先前程序中的方法中指定的程式碼中加入下列程式碼:

    Dim buildPropertyStorage As IVsBuildPropertyStorage = CType(hierarchy, IVsBuildPropertyStorage)
    If Not buildPropertyStorage Is Nothing Then
        buildPropertyStorage.SetItemAttribute(itemId, "Author", "Tom")
    End If
    
    IVsBuildPropertyStorage buildPropertyStorage =
        hierarchy as IVsBuildPropertyStorage;
    if (buildPropertyStorage != null)
    {
        buildPropertyStorage.SetItemAttribute(itemid, "Author", "Tom");
    }
    

若要驗證的屬性都會保留

  1. 開始Visual Studio再開啟或建立解決方案。

  2. 選取的專案項目中的 VsPkg.cs 方案總管] 中

  3. 使用中斷點,否則判斷在載入您的 VSPackage,並執行該 SetItemAttribute。

    注意事項注意事項

    您可以自動載入套件 UI 內容中的 VSPackage UICONTEXT_SolutionExists。如需詳細資訊,請參閱 How to: 自動載入套件 VSPackage

  4. 關閉Visual Studio ,然後開啟 「 記事本 」 中的專案檔。 您應該會看到 <Author> 標記值,如下所示:

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

請參閱

其他資源

專案項目 (Visual Studio SDK)