Helixoft Blog

Peter Macej - lead developer of VSdocman - talks about Visual Studio tips and automation

Getting "Find in Files" Search Results programmatically

Recently, I was asked an interesting question. How to get search results of Find In Files operation in VS? You can get these results quite easily from your macro or add-in. The trick is to read the contents of Find Results 1 or Find Results 2 window, whichever was used to show the results. So, the first thing is to determine which of them was used. We do it right after manually performed search from menu or our call of DTE.ExecuteCommand("Edit.FindinFiles"). The DTE.Find.ResultsLocation property will tell us this. Then we find the window and get its content.

The whole code which gets results of the last Find In Files operation is:

Dim resultsWin As Window = DTE.Windows.Item(DTE.Find.ResultsLocation)
Dim sel As TextSelection = resultsWin.Selection
sel.SelectAll()
Dim searchResults As String = sel.Text

You can use this approach to read the contents of other VS windows too. Just investigate EnvDTE.Constants as parameters for DTE.Windows.Item function. But remember that not all windows objects have set their Selection property directly. For example the Output window may have several panes. To get the text from active pane of Output window use this code:

Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim oWin As OutputWindow = win.Object
Dim sel As TextSelection = oWin.ActivePane.TextDocument.Selection
sel.SelectAll()
Dim outputText As String = sel.Text

And finally, you want to read the text quietly, without modifying current cursor or selection. Our methods select all text. So it would be good to restore original selection. To do so, add this code before sel.SelectAll():

'remember selection
Dim oldAnchor, oldActive As EnvDTE.TextPoint
oldAnchor = sel.AnchorPoint.CreateEditPoint
oldActive = sel.ActivePoint.CreateEditPoint

And after reading the text by sel.Text add this code:

'restore selection
sel.MoveToAbsoluteOffset(oldAnchor.AbsoluteCharOffset) 'set active point
sel.SwapAnchor() 'set anchor to active point
sel.MoveToAbsoluteOffset(oldActive.AbsoluteCharOffset, True)

 

Comments

# Avi 2006-05-08 07:14
Is it possible to update the text in the window ?
# Peter Macej 2006-05-08 09:35
It depends on which window you want to update. Once you have TextSelection object (in sel variable), you can use its methods to modify the text, e.g. sel.Insert("MY TEXT"). However, many windows are read only, including Search Results. Insert method throws an exception for them. I don't believe it's possible to modify them with DTE methods. On the other hand, you can easily write to Output window, see http://www.knowdotnet.com/articles/outputwindow.html .
# Avi 2006-05-08 10:23
The serach results is the window I need . I would like to edit the results and to short up the path.
If my path is c:\projects\xxx \yyy\zzz\ppp\cl ass.cs I would like to display ppp\class.cs.
There is a way to modify the results from the registry but It can either display full path or not. Can't edit the results.
That's on my wish list. Not possible in reasonable effort ?
# Peter Macej 2006-05-08 10:29
It is surely possible using VSIP but I've never used it so I cannot help here. Try to ask on http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=57&SiteID=1 MS forum or even better in http://groups.yahoo.com/group/vsnetaddin/ mailgroup. You can search this mailgroup, maybe this question was answered before.
# Jacky 2007-03-20 07:40
Sometimes, the codes work well, but sometimes it doesn't work.And I found the sel is null after call TextSelection sel = winFind.Selecti on as TextSelection; if it doesn't work.I don't know why
# Prakash Gupta 2009-12-04 06:09
I am uploading word,pdf files and storing these file in a drive(C) within a folder and store path of this folder into database.
I want to search any word within document(word,p df files) and it should display all the files(word,pdf files) containing that word.

any help

Thanks
Prakash Gupta

Comments are now closed for this entry

 

Start generating your .NET documentation now!
DOWNLOAD
Free, fully functional trial