Collapse All Folders in VS Solution Explorer

posted on Thursday, February 02, 2006 7:34 PM by kfinley

UPDATE: Now working in Visual Studio 2005 (not yet tested in 2008, but soon)

Here is a little macro I've been using for a while that I have honestly don't think I could live without at this point. One the most annoying things while working within a large solution in visual studio is how the solution tree can get expanded to the point that you waste time trying to either collapse folders or visually dig through the projects in order to find a file. I typically have lots of files open from projects within a solution and at times I like to just do a mental refresh and close all the open files I have. That's great except that I'm still starring at most of the files and folders in the solution explorer. With this macro added to Visual Studio all I have to do is click a button and every folder and it's child folders collapse in the explorer.

I'm not the first person to come up with something like this. The original code for this macro came from the article CollapseAll Macro for Visual Studio .NET found on The Code Project. I made modifications to the code so it would collapse all the folders in a project, not just the root project folder. It isn't super fast for large solutions / projects but it works. One minor issue in the macro is that the folder icons don't get updated when the node is closed. Not a big deal to me but just wanted to point that out.

Unfortunately the code doesn't work with the new solution folders in Visual Studio 2005. If your project doesn't include any solution folders it works fine. I'll have an updated version posted once I get the fix done. If anyone else solves the problem let me know.

Update: There is a bug in Visual Studio 2005 around solution folders that prevents the Expanded property from retaining the value you give it. In order to get it to work correctly I had to add a block to the CollapseMe method. I got the tip from the MSDN forum. Funny how many of these macros are floating around the web.

Imports EnvDTE
Imports System.Diagnostics

Public Module Personal

Sub CollapseAll()

'DESCRIPTION: Collapse all the nodes in the project tree

' Get the the Solution Explorer tree
Dim oSolutionExplorer As UIHierarchy
oSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).
Object()

' Check if there is any open solution
If (oSolutionExplorer.UIHierarchyItems.Count = 0) Then
Return
End If

' Get the top node (the name of the solution)
Dim oRootItem As UIHierarchyItem
oRootItem = oSolutionExplorer.UIHierarchyItems.Item(1)
Dim oChildItem As UIHierarchyItem

' Collapse each project node
For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem, oSolutionExplorer)
Next

' Select the solution node, or else when you click on the solution window
' scrollbar, it will synchronize the open document with the tree and pop
' out the corresponding node which is probably not what you want.
oRootItem.Select(vsUISelectionType.vsUISelectionTypeSelect)

End Sub


Sub
CollapseMe(ByVal oRootItem As UIHierarchyItem, ByVal oSolutionExplorer As UIHierarchy)

Dim oChildItem As UIHierarchyItem

For Each oChildItem In oRootItem.UIHierarchyItems
CollapseMe(oChildItem, oSolutionExplorer)
Next

oRootItem.UIHierarchyItems.Expanded = False

' Added to deal with the Visual Studio bug
If (oRootItem.UIHierarchyItems.Expanded = True) Then
oRootItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
oSolutionExplorer.DoDefaultAction()
End If

End Sub

End Module

Now Playing: Radiohead - Kid A - Idioteque

re: Collapse All Folders in VS Solution Explorer

Monday, April 09, 2007 9:02 AM by ryan
thank you thank you thank you

re: Collapse All Folders in VS Solution Explorer

Tuesday, February 12, 2008 1:47 AM by AJ
Tested in VS 2008 Team Edition, the macro works great, but the expanded bug is still there...

Faster Collapse All Folders in VS Solution Explorer

Tuesday, April 22, 2008 7:06 AM by Rajiv
If we wrap this recursive call

CollapseMe(oChildItem, oSolutionExplorer)

in a check

If (oChildItem.UIHierarchyItems.Expanded = True) Then

then the macro runs MUCH faster. It does not reach into closed nodes with open children, but oh well.

re: Collapse All Folders in VS Solution Explorer

Friday, May 29, 2009 1:23 AM by Anil Tumati
Awesome. I was so bugged up with this issue from last so many days. Now I am code peacefully with just a button click to close and open what I need to see.

re: Collapse All Folders in VS Solution Explorer

Wednesday, September 30, 2009 12:37 AM by Jean-Pierre Fouche
Does not work for me. Copied and pasted into a module; Assigned Keyboard shortcut; Does not seem to have any effect; nor does it step into debug...

re: Collapse All Folders in VS Solution Explorer

Saturday, October 03, 2009 10:17 AM by kfinley
@Jean-Pierre Fouche: Not sure what your issue is. This macro does indeed work. If you have a break point in the module and it is never even hitting it that tells me the macro is not running. I recommend you run the macro first by hand to make sure it is working and then once it is setup the keyboard shortcut. I use both keybord shortcut and a menubar icon for this so I know it works just fine.

Are you using VS2k8? I have not moved this into VS2k10 yet so that could also be your issue.

EnlildTrodo

Sunday, August 28, 2011 12:35 PM by Wreletritty
StoomoRooraps <a href=kuygkyudu.com>jufufduy</a> Dromfotssog

re: Collapse All Folders in VS Solution Explorer

Thursday, October 06, 2011 6:43 AM by Tilly
Why does this have to be the ONLY reliable soruce? Oh well, gj!

In rear thumbs convince naked.

Thursday, January 05, 2012 2:59 AM by Bypenisse
Then <a href=http://papoziw.abgefahrene-website.de/novolog-flexpen-patient-assistance/>flexpen size</a> meeting ill <a href=http://papoziw.abgefahrene-website.de/pantoprazole-$4.00/>pantoprazole 10</a> peaceful unmistakably <a href=http://papoziw.abgefahrene-website.de/bactroban-2-cream/>bactroban ointment used for</a> tubes laugh <a href=http://papoziw.abgefahrene-website.de/novation-hospira-heparin/>hospira</a> state elegantly <a href=http://papoziw.abgefahrene-website.de/what-is-malarone/>buy malarone</a> relative clean <a href=http://papoziw.abgefahrene-website.de/berna-products/>berna dean he's mine</a> recruiting driven <a href=http://papoziw.abgefahrene-website.de/bacteriostatic/>bacteriostatic saline v s lidocaine</a>

аська для телефона бесплатно Lieds

Tuesday, January 10, 2012 5:29 AM by Brula
<a href="http://icqmobilephones.net/">icq">http://icqmobilephones.net/">icq на телефон</a>
http://icqmobilephones.net/ - скачать icq на телефон nokia

Buy Cigarettes Online absowl

Thursday, January 19, 2012 12:50 PM by Buy Cigarettes Online si
http://buy-cigaretteonline.com/ - Buy Cheap Cigarettes Online Dutyfree Tobacco <a href="http://buy-cigaretteonline.com/">Buy Cheap Cigarettes Online Dutyfree Tobacco</a> <a href="http://buy-cigaretteonline.com/george-karelias-and-sons-rolling-tobacco.html">George Karelias & Sons Rolling Tobacco - Dutyfree Tobacco Buy Cheap Cigarettes Online</a> <a href="http://buy-cigaretteonline.com/gb.html">G.B. - Dutyfree Tobacco Buy Cheap Cigarettes Online</a> http://buy-cigaretteonline.com/parliament_blue_ks.html - Parliament Blue KS (Swiss Made) - Dutyfree Tobacco Buy Cheap Cigarettes Online http://buy-cigaretteonline.com/chisum-full-flavor-filtered-little-cigar.html - Chisum Full Flavor Filtered Little Cigar - Dutyfree Tobacco Buy Cheap Cigarettes Online

Hello!

Wednesday, February 01, 2012 9:10 PM by nuakypyncbype
Hi! my rank is Jully. I would like to meemeet respected boy :)
This is my homepage - http://jskdh5jkd7djh4.com/l