Author: Jeff Noble
The Problem
While working on a project, I wanted to have my partial class files that I created to be indented in the solution explorer under their dependant classes. The same way that a Form or UserControl makes a child node in the tree for it’s .designer.cs and .resx files.
When adding my own files, I could not figure out how to indent them. While searching, I found two solutions.
The Solution
You can edit the project file <yourproject>.csproj. To make a cs file a dependent, find the file you want to be the parent. It should look this this (your file name will be different of course):
<Compile Include=”Views\CallCenterView\CallCenterViewPresenter.cs” />
(yours may or may not include a SubType node, it doesnt matter).
Then find the line that holds the one you want to be the child file:
<Compile Include=”Views\CallCenterView\CallCenterViewPresenter.Order.cs”/>
Then add the <DependentUpon> node to the mix on the child nodes ONLY. Like this:
(Replace the above child line with the following)
<Compile Include=”Views\CallCenterView\CallCenterViewPresenter.Order.cs”>
<DependentUpon>CallCenterViewPresenter.cs</DependentUpon>
</Compile>
You can add as many of these as you like. Save and refresh the project in the VS IDE and you will see your efforts.
I also found this code posted on a forum and I HAVE NOT TESTED IT!! USE IT AT YOUR OWN RISK (but it looks safe at first glance).
This comes from bdaniel7 on the microsoft forums.
The code below is from an add-in I developed, add-in which generates some files and adds them to a C# project and makes them dependent of the file they were generated from. I’m using it in VS 2005 Pro and it works wery well. Create a new Add-in project in VS 2005 and you’ll see what are all classes presented here (DTE2, Project and so on); Of course, you can refine this example further…:P
private AddFileToProject( string tempFileName, bool ask )
{
private DTE2 _applicationObject;
Project currentProject =
_applicationObject.SelectedItems.Item( 1 ).ProjectItem.ContainingProject as Project;
localFile = Path.Combine(
Path.GetDirectoryName( currentProject.FullName ),
Path.GetFileName( tempFileName ) );
bool localFileExists = File.Exists( localFile );
if ( !ask ||
(ask && !localFileExists) ||
( ask && localFileExists && ShowOverwriteDialog( localFile ) == DialogResult.Yes ) )
{
// copy the file in the project directory
File.Copy( tempFileName, localFile, true );
// add to project
ProjectItem item = _applicationObject.SelectedItems.Item( 1 ).ProjectItem;
// add as “DependentUpon”
item.ProjectItems.AddFromFile( localFile );
// expand the files below the selected item
item.ExpandView();
// save the project
currentProject.Save( currentProject.FullName );
}
}bdaniel7
