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 / Extensibility / October 2004

Tip: Looking for answers? Try searching our database.

ADD-IN: Search for string in file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oleg Ogurok - 18 Oct 2004 20:53 GMT
Hi all,

How do I search for a keyword in an entire C# (.cs) document?

e.g.,
EnvDTE.ProjectItem item = project.ProjectItems.Item(1);

item.Document returns the document, but I can't seem to figure out how to
search it.

document.Selection returns only the current selection.

Document.MarkText finds the text and sets bookmarks on those lines, but I
can't even figure out how to move the cursor there.

Is there a simple way to either search the entire text or get TextSelection
for the entire document?

Thanks.

-Oleg.
Carlos J. Quintero [MVP] - 19 Oct 2004 14:22 GMT
Get Document.Object("TextDocument"), cast to TextDocument, and use
TextDocument.StartPoint.CreateEditPoint.FindPattern(...)

Signature

Carlos J. Quintero (Visual Developer - .NET MVP)

The MZ-Tools all-in-one add-in, now for .NET: http://www.mztools.com

> Hi all,
>
[quoted text clipped - 17 lines]
>
> -Oleg.
Oleg Ogurok - 19 Oct 2004 16:37 GMT
Thanks, Carlos.

However, FindPattern() always returns False.
Here's my code:

   foreach (EnvDTE.ProjectItem prjItem in project.ProjectItems)
   {
    if (prjItem.Name == "AssemblyInfo.cs")
    {
     prjItem.Open("{00000000-0000-0000-0000-000000000000}");

     //string pattern = "\\[assembly\\:
AssemblyVersion\\(\\\"([^\\\"]+)\\\"\\)\\]";
     // Let's try a simple search pattern first
     string pattern = "AssemblyVersion";

     TextDocument textDocument =
(TextDocument)prjItem.Document.Object("TextDocument");
     EditPoint editPoint = textDocument.StartPoint.CreateEditPoint();
     TextRanges tags = null;
     EditPoint endPoint = null;
     if (editPoint.FindPattern(pattern,
      (int)vsFindOptions.vsFindOptionsFromStart +
(int)vsFindOptions.vsFindOptionsRegularExpression,
      ref endPoint, ref tags))    // Always returns false.

Any help is gladly appreciated.

-Oleg.

> Get Document.Object("TextDocument"), cast to TextDocument, and use
> TextDocument.StartPoint.CreateEditPoint.FindPattern(...)
[quoted text clipped - 21 lines]
>>
>> -Oleg.
Carlos J. Quintero [MVP] - 20 Oct 2004 13:36 GMT
EditPoints are a bit tricky. The FindPattern needs an input/output edit
point for the 3rd parameter. Take a look at this macro:

       Dim objProjectItem As ProjectItem
       Dim objTextDocument As TextDocument
       Dim objWindow As Window
       Dim objEditPoint1 As EditPoint
       Dim objEditPoint2 As EditPoint

       For Each objProjectItem In
DTE.Solution.Projects.Item(1).ProjectItems

           If (objProjectItem.Name = "AssemblyInfo.cs") Then

               objWindow =
objProjectItem.Open(EnvDTE.Constants.vsext_vk_TextView)

               objTextDocument =
DirectCast(objProjectItem.Document.Object("TextDocument"), TextDocument)

               objEditPoint1 = objTextDocument.StartPoint.CreateEditPoint()
               objEditPoint2 = objTextDocument.EndPoint.CreateEditPoint()

               If objEditPoint1.FindPattern("AssemblyVersion",
vsFindOptions.vsFindOptionsNone, objEditPoint2, Nothing) Then

                   MsgBox("Found " & objEditPoint1.GetText(objEditPoint2))

               End If

           End If

       Next

Regards,

Carlos J. Quintero (Visual Developer - .NET MVP)

The MZ-Tools all-in-one add-in, now for .NET: http://www.mztools.com

> Thanks, Carlos.
>
[quoted text clipped - 25 lines]
>
> -Oleg.

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.