Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Visual Studio.NET / IDE / February 2006

Tip: Looking for answers? Try searching our database.

VS2005 Automation: How to find active document item in Solution Explorer hierarchy?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Alex Blekhman - 29 Jan 2006 19:14 GMT
Hello,

I'm writing macro for VS2005 and I need to find
programmatically currently active document in Solution
Explorer hierarchy. "Track Active Item in Solution Explorer"
option is OFF, so I need to figure out solution path for
active document.

I can easily find file path of active document:

   DTE.Windows.ActiveDocument.FullName

However, I cannot translate this path intoto solution path.
I tried to record macro selecting solution element. It
records something like that:

   DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate()
   DTE.ActiveWindow.Object.GetItem("Solution\Project\Folder\File.cpp")._
       Select(vsUISelectionType.vsUISelectionTypeSelect)

I.e., item's path is already known for macro recorder.

How can I find solution path for currently active document?

Thanks in advance
Alex
murali - 30 Jan 2006 07:15 GMT
hai

i need how to create dll file. what are the topic i prepare.  give the book
name and liks

reagards

murali
Carlos J. Quintero [VB MVP] - 01 Feb 2006 16:50 GMT
Hi Alex,

First, I recommend to turn Option Strict On because there are things in you
post or code that won´t even compile such as DTE.Windows.ActiveDocument.

That said, UIHierarchy.GetItem(path) (what you call
ActiveWindow.Object.GetItem) takes as path the names of the items in the
solution explorer, so it won´t help you for your purpose, specially if
virtual folders are involved. Instead you need to do the following:

- Traverse recursively UIHierarchy.UIHierarchyItems and
UIHierarchyItem.UIHierarchyItems until you find the target.
- Each UIHierarchyItem has a UIHierarchyItem.Object property that can
return:

a) An EnvDTE.SolutionClass for the solution (only in VS 2005, since VS.NET
2002/2003 returned nothing for this case)
b) An EnvDTE.Project for a project or top-level solution folder.
c) An EnvDTE.ProjectItem for a file or non top-level folder
d) Some strange object to ignore

Notice that a file can belong to two projects, so you should target in the
Solution Explorer both ProjectItem.ContainingProject and then ProjectItem.

So, once you get the object you cast it to the proper class and you get, say
Project.FullName to compare to your project or ProjectItem.FileNames(...) to
compare with DTE.ActiveDocument.FullName and find the target.

I have written all that from the top of my head, but the approach works, I
use it in my add-in (below) for the locators (solution, folder, project,
file) of some features like the statistics window if you want to try.

Signature

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com

> Hello,
>
[quoted text clipped - 20 lines]
> Thanks in advance
> Alex
Alex Blekhman - 03 Feb 2006 10:44 GMT
Carlos, thanks for elaborate answer. I turned on Strict
option for the module. It caused a couple of compilation
errors in module, however all of them were easily fixable.

Currently I was doing solution traversing, too, however I
have two major problems with that:

1. I can't reliably detect whether some UIHierarchyItem can
be container of other items, or is it a file (or whatever).

2. While traversing in solution tree, all solution folders
get expanded. I don't know how to prevent it. Also,
traversing is taking ages with big projects (on my P4-3.4GHz
machine it can take up to 5 minutes and more for our
project). I reckon slow traversing is caused by item
expanding.

Then the question remains, how "Track Active Item in
Solution Explorer" option does it so fast? I need to achieve
similar effect. Here is the code I use to list all solution
items to output pane:

-------
Sub ListSolutionItems()

   OutputHierarchyItems( _
       GetNamedOutputPane("List Solution Items"), _
       DTE.ToolWindows.SolutionExplorer.UIHierarchyItems)

End Sub

Sub OutputHierarchyItems( _
   ByRef owPane As OutputWindowPane, _
   ByRef uihItems As UIHierarchyItems, _
   Optional ByVal nIntend As Integer = 0)

   Dim uihItem As UIHierarchyItem

   For Each uihItem In uihItems
       Dim sOut As String = uihItem.Name
       Dim bContainer As Boolean = _
           IsItemsContainer(uihItem.Object)

       If bContainer Then
           sOut = "[" & sOut & "]"
       End If

       owPane.OutputString( _
           StrDup(nIntend, vbTab) & sOut & " - " & _
           uihItem.UIHierarchyItems.Expanded & vbCrLf)

       If bContainer Then
           OutputHierarchyItems( _
               owPane, _
               uihItem.UIHierarchyItems, _
               nIntend + 1)
       End If
   Next
End Sub

Function IsItemsContainer( _
   ByRef uihItem As Object) As Boolean

   IsItemsContainer = _
       (TypeOf uihItem Is EnvDTE80.SolutionFolder) Or _
       (TypeOf uihItem Is EnvDTE.SolutionClass) Or _
       (TypeOf uihItem Is EnvDTE.ProjectItem)
End Function

Function GetNamedOutputPane( _
   ByVal sName As String) As OutputWindowPane

   Dim OW As OutputWindow = DTE.ToolWindows.OutputWindow
   Dim OWPane As OutputWindowPane

   For Each OWPane In OW.OutputWindowPanes
       If OWPane.Name = sName Then
           GetNamedOutputPane = OWPane
           Exit Function
       End If
   Next

   GetNamedOutputPane = OW.OutputWindowPanes.Add(sName)
End Function
-------
Carlos J. Quintero [VB MVP] - 03 Feb 2006 12:41 GMT
Hi Alex,

I can confirm that navigating the solution explorer opens closed folders,
even the "Track Active Item" feature of VS does that effect. I am not sure
if it is "by design" or a "bug", but certainly it is undesirable because the
"Track Active Item" feature will open lots of folders in other hives until
it finds the target item. Could you open a bug here to hear an answer from
Microsoft?:

http://lab.msdn.microsoft.com/ProductFeedback/

About the slowness, I don´t know, but 5 minutes is a huge time. How many
projects and files has your solution?

Signature

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com

> Carlos, thanks for elaborate answer. I turned on Strict
> option for the module. It caused a couple of compilation
[quoted text clipped - 81 lines]
> End Function
> -------
Alex Blekhman - 03 Feb 2006 14:33 GMT
> I can confirm that navigating the solution explorer opens
> closed folders, even the "Track Active Item" feature of
> VS does that effect. I am not sure if it is "by design"
> or a "bug", but certainly it is undesirable because the
> "Track Active Item" feature will open lots of folders in
> other hives until it finds the target item.

Actually, "Track Active Item" feature doesn't open
irrelevant folders. Only my traversion does. Also, "Track
Active Item" selects active item in solution pane really
fast. As if it already knew solution path of active item.

> Could you
> open a bug here to hear an answer from Microsoft?:
>
> http://lab.msdn.microsoft.com/ProductFeedback/

Done.
"Traversing Solution Explorer tree via VBA macro expands all
items"
Bug ID: FDBK45186

> About the slowness, I don´t know, but 5 minutes is a huge
> time. How many projects and files has your solution?

It's huge ancient project consisted of millions of LOC nad
several tens of thousands files (I never counted exact
numberm though). I think delay is caused because of source
control status that gets updated when item is expanded.
Actually, IDE is completely unresponsive for several
minutes, then it becomes clunky while dumps all these
filenames in output pane. When I'll get to office by Sunday
I'll try to run it without output, too.
Alex Blekhman - 05 Feb 2006 16:46 GMT
> When I'll get to office by
> Sunday I'll try to run it without output, too.

I run it without output. It completed a lttle bit faster,
but not signigicantly. Items are expanded anyway, though.
Carlos J. Quintero [VB MVP] - 06 Feb 2006 08:49 GMT
> Actually, "Track Active Item" feature doesn't open irrelevant folders.

It did it in my test...

> It's huge ancient project consisted of millions of LOC nad
> several tens of thousands files (I never counted exact
> numberm though).

Ah, that's really huge...

Signature

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.