Click here to Skip to main content
13,295,691 members (36,902 online)
Click here to Skip to main content
Add your own
alternative version


253 bookmarked
Posted 16 Jun 2005

Desktop Search Application: Part 1

, 6 Jul 2006
Rate this:
Please Sign up or sign in to vote.
Building an application that searches your Office documents in tenths of a second.

Sample Image - screenshot1.gif


Let's take the following exercise: Build a C# application that instantly searches your Documents folder. We need the application to:

  • Search the contents of Office documents (Word, Excel, PowerPoint). I.e., XLS, DOC, PPT files.
  • Search the contents of HTML documents.
  • Search the contents of text documents.
  • Search quickly (i.e., give results in less than a second).
  • Allow to open the documents directly from the results list.

Update: This Desktop Search application is now part of the Seekafile Server 1.0 - Open-Source Indexing Server. The server provides automatic indexing on the background, and you can search the index through a Windows Forms client search application. See also the Seekafile Server roadmap.

Task 1: Full-Text Indexing

For the indexing and searching, we will use an excellent search engine called DotLucene. It's a C# port of Java Lucene, maintained by George Aroush. It has many great features:

  • Very good performance.
  • Ranked search results.
  • Search query highlighting in results.
  • Searches structured and unstructured data.
  • Metadata searching (query by date, search custom fields...).
  • Index size approximately 30% of the indexed text.
  • Can also store fully indexed documents.
  • Pure managed .NET in a single assembly.
  • Very friendly licensing (Apache Software License 2.0).
  • Localizable (support for Brazilian, Czech, Chinese, Dutch, English, French, German, Japanese, Korean, and Russian included in the DotLucene National Language Support Pack).
  • Extensible (source code included).

For more details on creating the index and searching, see my previous article: DotLucene: Full-Text Search for Your Intranet or Website using 37 Lines of Code.

Task 2: Parsing Office Documents

As DotLucene can index only plain text, we need to parse the Office documents and extract text from them. Reading their binary structure isn't an easy job. However, on Windows 2000+, we can use the IFilter interface which is a part of the Windows Indexing Service. This is installed by default on all Windows 2000+ systems (no Office installation is required).

The IFilter API is also being used by the Windows Desktop Search (MSN Search Toolbar) and Lookout, so you don't have to be afraid that we will use something obscure to parse the documents. It can also parse other file types if you install the appropriate filter.

Working with the IFilter interface requires a lot of COM interop which is a bit tricky. After tweaking the samples available on the web, I finally had a code that worked correctly in most cases:

public static string Parse(string filename)
  IFilter filter = null;
  try {
    StringBuilder plainTextResult = new StringBuilder();
    filter = loadIFilter(filename); 
    STAT_CHUNK ps = new STAT_CHUNK();
    IFILTER_INIT mFlags = 0;
    uint i = 0;
    filter.Init( mFlags, 0, null, ref i);
    int resultChunk = 0;
    resultChunk = filter.GetChunk(out ps);
    while (resultChunk == 0)
      if (ps.flags == CHUNKSTATE.CHUNK_TEXT)
        uint sizeBuffer = 60000;
        int resultText = 0;
        while (resultText == Constants.FILTER_S_LAST_TEXT || resultText == 0)
          sizeBuffer = 60000;
          System.Text.StringBuilder sbBuffer = 
             new System.Text.StringBuilder((int)sizeBuffer);
          resultText = filter.GetText(ref sizeBuffer, sbBuffer);
          if (sizeBuffer > 0 && sbBuffer.Length > 0)
            string chunk = sbBuffer.ToString(0, (int)sizeBuffer);
      resultChunk = filter.GetChunk(out ps);
    return plainTextResult.ToString();
    if (filter != null)

Assembling the Application

In short:

  • The index can only be built from scratch.
  • We are not returning the sample of the found document.
  • We are skipping the files that can't be parsed successfully.
  • We are loading the associated explorer icon for each document in the results.
  • You can choose the folder to be indexed; by default, it's your Documents folder.
  • The indexed file types are hard-coded (txt, htm/html, doc, xls, ppt).
  • The index is stored in your profile in the Local Settings/Application Data/DesktopSearch folder.
  • Remember that it is possible to search while the indexing is in progress.


Some statistics (Athlon XP 2000+, 1GB RAM, Seagate SATA drive 7200 RPM):

  • Documents indexed: 1185
  • Total size of indexed documents: 120,690,622 bytes
  • Rebuilding the index took: 5 minutes 55 seconds
  • Index size: 4,339,950 bytes
  • Search time (including the rendering on display): from 0.0937 seconds (25 found items) to 0.3125 seconds (213 found items)

To Be Continued...

In the next part of the article, we will extend this simple application with the following features:

  • The indexing will be handled by a separate application which will update the index continuously on the background.
  • The results will contain a sample of the document with highlighted query words.
  • We will index and search the file name and the last modified date.

Resources and Acknowledgements

Search Engine:

Seekafile Server - Open-Source Indexing Server

Office Documents Parsing:



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Dan Letecky
Czech Republic Czech Republic

You may also be interested in...


Comments and Discussions

GeneralRe: How to search text in Visio documents Pin
Jerome Barras1-Mar-06 3:20
memberJerome Barras1-Mar-06 3:20 
GeneralRe: How to search text in Visio documents Pin
Vijay Kumar Raja Grandhi20-Feb-06 1:56
memberVijay Kumar Raja Grandhi20-Feb-06 1:56 
GeneralMaybe a Critical Problem Pin
Spongesong14-Jan-06 7:55
memberSpongesong14-Jan-06 7:55 
GeneralRe: Maybe a Critical Problem Pin
Inspector27-Jan-06 9:03
memberInspector27-Jan-06 9:03 
GeneralSame problem Pin
Spongesong31-Jan-06 19:31
memberSpongesong31-Jan-06 19:31 
GeneralRe: Same problem Pin
Inspector1-Feb-06 6:27
memberInspector1-Feb-06 6:27 
GeneralDelete item from index Pin
mbowles20113-Jan-06 4:06
membermbowles20113-Jan-06 4:06 
GeneralRe: Delete item from index Pin
Dan Letecky13-Jan-06 4:16
memberDan Letecky13-Jan-06 4:16 
GeneralRe: Delete item from index Pin
mbowles20113-Jan-06 4:29
membermbowles20113-Jan-06 4:29 
GeneralRe: Delete item from index Pin
Vijay Kumar Raja Grandhi20-Feb-06 1:28
memberVijay Kumar Raja Grandhi20-Feb-06 1:28 
GeneralRe: Delete item from index Pin
Vijay Kumar Raja Grandhi20-Feb-06 1:28
memberVijay Kumar Raja Grandhi20-Feb-06 1:28 
GeneralRe: Delete item from index Pin
mbowles20121-Feb-06 4:03
membermbowles20121-Feb-06 4:03 
GeneralRe: Delete item from index Pin
Vijay Kumar Raja Grandhi21-Feb-06 4:16
memberVijay Kumar Raja Grandhi21-Feb-06 4:16 
Generalmissing matches Pin
novalis7827-Nov-05 1:32
membernovalis7827-Nov-05 1:32 
QuestionVB Code? Pin
morgen19-Nov-05 17:08
membermorgen19-Nov-05 17:08 
AnswerRe: VB Code? Pin
FunkyMonkey18-Jan-06 20:14
memberFunkyMonkey18-Jan-06 20:14 
GeneralRe: VB Code? Pin
VbGuru61317-Mar-06 4:15
memberVbGuru61317-Mar-06 4:15 
QuestionHow to read properties using IFilter? Pin
sandeep833-Nov-05 5:54
membersandeep833-Nov-05 5:54 
AnswerRe: How to read properties using IFilter? Pin
Jake_4215-Nov-05 7:46
memberJake_4215-Nov-05 7:46 
GeneralRe: How to read properties using IFilter? Pin
k0ski27-Nov-05 9:12
memberk0ski27-Nov-05 9:12 
GeneralRe: How to read properties using IFilter? Pin
Dr. Bonne21-Dec-05 9:53
memberDr. Bonne21-Dec-05 9:53 
GeneralRe: How to read properties using IFilter? Pin
Jake_4222-Dec-05 10:53
memberJake_4222-Dec-05 10:53 
QuestionRe: How to read properties using IFilter? Pin
Inspector18-Jan-06 12:05
memberInspector18-Jan-06 12:05 
AnswerRe: How to read properties using IFilter? Pin
Jake_4219-Jan-06 7:11
memberJake_4219-Jan-06 7:11 
GeneralRe: How to read properties using IFilter? Pin
Inspector19-Jan-06 8:17
memberInspector19-Jan-06 8:17 
QuestionRe: How to read properties using IFilter? Pin
Ramakrishnan Seerangasamy5-Jun-11 21:45
memberRamakrishnan Seerangasamy5-Jun-11 21:45 
GeneralRe: How to read properties using IFilter? Pin
Erik238023-Jan-06 12:44
memberErik238023-Jan-06 12:44 
GeneralRe: How to read properties using IFilter? Pin
Gwynge8-Feb-06 2:00
memberGwynge8-Feb-06 2:00 
GeneralRe: How to read properties using IFilter? Pin
tprakash13-Jan-09 23:16
membertprakash13-Jan-09 23:16 
QuestionPart 2 ?? Pin
mbowles20114-Sep-05 4:32
membermbowles20114-Sep-05 4:32 
AnswerRe: Part 2 ?? Pin
mbowles20123-Sep-05 9:56
membermbowles20123-Sep-05 9:56 
GeneralRe: Part 2 ?? Pin
Dan Letecky25-Sep-05 23:07
memberDan Letecky25-Sep-05 23:07 
GeneralRe: Part 2 ?? Pin
mbowles20126-Sep-05 3:48
membermbowles20126-Sep-05 3:48 
QuestionRe: Part 2 ?? Pin
Vijay Kumar Raja Grandhi20-Feb-06 6:05
memberVijay Kumar Raja Grandhi20-Feb-06 6:05 
GeneralI can develop program use thai language Pin
weewat31-Aug-05 20:19
sussweewat31-Aug-05 20:19 
QuestionHow to support thai language Pin
weerawat09@hotmail.com9-Aug-05 1:01
memberweerawat09@hotmail.com9-Aug-05 1:01 
GeneralSearch Text High-Lighting Pin 21:09 21:09 
GeneralRe: Search Text High-Lighting Pin
Dan Letecky13-Jan-06 4:19
memberDan Letecky13-Jan-06 4:19 
GeneralThe project set up Pin
Dan Letecky25-Jul-05 6:46
memberDan Letecky25-Jul-05 6:46 
QuestionHow to get Index data. Pin
BhatiaWorld20-Jul-05 21:15
memberBhatiaWorld20-Jul-05 21:15 
Questionhow to support chinese Pin
lib10068-Jul-05 4:13
susslib10068-Jul-05 4:13 
AnswerRe: how to support chinese Pin
bevincent2-Aug-05 21:31
memberbevincent2-Aug-05 21:31 
GeneralProblem with Pathname > 260 Chars Pin
bmarner7-Jul-05 4:35
memberbmarner7-Jul-05 4:35 
GeneralRe: Problem with Pathname > 260 Chars Pin
myrealbruno12-Jul-05 23:33
membermyrealbruno12-Jul-05 23:33 
GeneralRe: Problem with Pathname > 260 Chars Pin
Anonymous14-Jul-05 1:27
sussAnonymous14-Jul-05 1:27 
GeneralRe: Problem with Pathname > 260 Chars Pin
myrealbruno14-Jul-05 4:34
membermyrealbruno14-Jul-05 4:34 
GeneralRe: Problem with Pathname > 260 Chars Pin
bmarner3-Aug-05 0:01
memberbmarner3-Aug-05 0:01 
GeneralRe: Problem with Pathname > 260 Chars Pin
Anonymous25-Aug-05 6:07
sussAnonymous25-Aug-05 6:07 
QuestionSearch text in .CS, .VB etc files ?? Pin
Anish Sharma6-Jul-05 4:07
memberAnish Sharma6-Jul-05 4:07 
Generalother IFilters Pin
myrealbruno4-Jul-05 7:25
membermyrealbruno4-Jul-05 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 6 Jul 2006
Article Copyright 2005 by Dan Letecky
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid