Friday, February 17, 2012

Dependent files in Visual Studio projects

If you have ever lost the hierarchical tree view in a Visual Studio project for a main file and its associated files, you probably already know that there is no way directly within the Visual Studio IDE to re-associate those files.  You may need to have this hierarchical tree view display particularly with the use of Config Transforms which are dependent on a primary Web.config file and its associated Build configuration transformation files.

Therefore, in order to do this, you will have to do the following:


  1. From within Visual Studio, right click on the affected project and select "Unload project"
  2. Once the project has been unloaded, you can right click on the project and select "Edit project"
  3. This will open the Visual Studio project for editing and display the MSBuild structure of the Visual Studio project file.
  4. If you are looking for a particular file such as Web.config, you can search for it.
  5. Once you find the appropriate element, determine if it exists as a standalone content item within an existing ItemGroup or if it is in its own associated ItemGroup.  If it is not within its own ItemGroup, you will need to remove the individual <Content Include="Web.config"> element and place in its own ItemGroup.  Visual Studio Intellisense will assist you in creating the new ItemGroup element.
  6. Once you have the Content item in the appropriate location, you need to add the dependency content items.
  7. A dependency content item will look like the following:

    <content include="Web.Debug.config">
    <dependentupon>Web.config</dependentupon>
    </content>

  8. You can add each of these elements within the dedicated ItemGroup depending on the number of Config Transforms that you have for your project.
  9. Your final ItemGroup should look similar to the following:

      <ItemGroup>
        <Content Include="Web.config" />
        <Content Include="Web.Debug.config">
          <DependentUpon>Web.config</DependentUpon>
        </Content>
        <Content Include="Web.Release.config">
          <DependentUpon>Web.config</DependentUpon>
        </Content>
      </ItemGroup>

No comments:

Post a Comment