Skip navigation

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

Advertisement

7 Comments

  1. Hi, you can safely use that code, i made it and used it without a problem.

    Daniel

  2. Thanks for the input!

    -Savij

  3. This was exactly what I needed. And it was easy to find! Thanks.

  4. The nuance I don’t care for in this solution is that you have to Show All Files in order to see the partial source files. Not a huge deal, but I wish there was a way to just group them in the project withouth requiring show all.

  5. I wish you could just drag the files in the ide, but we get what we get I guess….. Thanks for the post!
    -J

  6. Yes, we get what we get :)

    I did find a 3rd party addin that lets you group/ungroup
    http://www.mokosh.co.uk/page/VsCommands.aspx

  7. there’s an addin for Visual Studio 2010 that does that pretty well: http://visualstudiogallery.msdn.microsoft.com/en-us/d491911d-97f3-4cf6-87b0-6a2882120acf


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.