This documentation is archived and is not being maintained.

How to: Persist the Property of a Project Item

You may want to persist a property you add to a project item, such as the author of a source file. You can do this by storing the property in the project file.

The first step to persist a property in a project file is to obtain the hierarchy of the project as an IVsHierarchy interface. You can obtain this interface either by using Automation or by using IVsMonitorSelection. Once you obtain the interface, you can use it to determine which project item is currently selected. Once you have the project item ID, you can use SetItemAttribute to add the property.

In the following procedures, you persist the VsPkg.cs property Author with the value Tom in the project file.

To obtain the project hierarchy by using Automation

  • Add the following code to your VSPackage:

    No code example is currently available or this language may not be supported.

To persist the project item property by using Automation

  • Add the following code to the code given in the method in the previous procedure:

    No code example is currently available or this language may not be supported.

To obtain the project hierarchy by using IVsMonitorSelection

  1. Add the following code to your VSPackage:

    No code example is currently available or this language may not be supported.

To persist the selected project item property, given the project hierarchy

  • Add the following code to the code given in the method in the previous procedure:

    No code example is currently available or this language may not be supported.

To verify that the property is persisted

  1. Start Visual Studio and then open or create a solution.

  2. Select the project item VsPkg.cs in Solution Explorer.

  3. Use a breakpoint or otherwise determine that your VSPackage is loaded and that SetItemAttribute runs.

    Note Note

    You can autoload a VSPackage in the UI context UICONTEXT_SolutionExists. For more information, see How to: Autoload a VSPackage.

  4. Close Visual Studio and then open the project file in Notepad. You should see the <Author> tag with the value Tom, as follows:

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