Tuesday, June 5, 2012

Create Nested Files In Visual Studio Solution Explorer


We all are aware that visual studio solution explorer views the project files in a tree like structure. If we add a directory to the root of our project and place some files there, that files will be nested on that directory. Also, we know that this nesting capability is not only for directories. In web applications, .cs and .designer.cs file will be nested under .aspx file. If you worked with entity data model, you might have observed that model's designer.cs file is nested under the .edmx file.

Now, what if we would be able to nest our own files under our own created files. Isn't it crazy? well, not much here is how we can do it. Lets take a simple example project.

In the below screen-shot, you can observe the existing non-nested files.


Here IShape is an interface and Rectangle, Triangle, and Square are the classes derived from it. It would be helpful if we were able to see this relation in the solution explorer itself, isn't it? or it will be helpful, if we were able to browse the related files immediately. Well, we can do it by nesting the implementation files under the interface file.
  • Go to the folder on windows explorer and open your project file(.csproj) with notepad or any other text or xml editor.
  • You can see the ItemGroup under PropertyGroup. Here is mine:
  <ItemGroup>
    <Compile Include="IShape.cs" />
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Rectangle.cs" />
    <Compile Include="Square.cs" />
    <Compile Include="Triangle.cs" />
  </ItemGroup>
  • Modify it like this:
  <ItemGroup>
    <Compile Include="IShape.cs" />
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Rectangle.cs" >
 <DependentUpon>IShape.cs</DependentUpon>
    </Compile>
    <Compile Include="Square.cs" >
 <DependentUpon>IShape.cs</DependentUpon>
    </Compile>
    <Compile Include="Triangle.cs" >
 <DependentUpon>IShape.cs</DependentUpon>
    </Compile>
  </ItemGroup>
  1. Remove the / from compile tag of the file which you would like to nest.
  2. Wrap the parent file name with the tags <DependentUpon>, i.e <DependentUpon>ParentFileName.cs</DependentUpon> and place it under the file name which you would like to nest.
  3. Close the compile tag(</compile>) of the nested file and we are done!
  • This is the final screenshot of our solution explorer:

Note: This is not only limited for inheritance. You can do it for any files. Be careful while editing the project file. Improper edit of this file will corrupt your project from being open in visual studio.

5 comments:

  1. Thanks Naveen - just what I was looking for. VS2012 was refusing to nest the files generated by my T4 template. This sorted it out.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks seo expert

    ReplyDelete
  4. NFL Predictions – get free NFL picks for each NFL recreation, together with the Super Bowl. Local regulations prohibit us from permitting you to log in or place bets on our website. A proposition wager is a wager not tied to the result of|the outcomes of} a contest. If the final rating totals 6 precisely, it’s thought of a “push” and your authentic wager is returned to you. If you wager the spread on Seattle -6, they would wish to win by 7 factors or extra to “cover” and 로스트아크 thus you would win your wager. Additionally, each team within the contest is accompanied by a +/- subsequent to the spread number.

    ReplyDelete