4

I have a folder with 20 text files in it. and I wish to index this folder and search for any keyword among these files.

How can I do this in C#?

Justin
  • 84,773
  • 49
  • 224
  • 367
Bhagyashree
  • 55
  • 1
  • 1
  • 2
  • 1
    What about "not with lucene"? Any reason you willfully ignore what Windows offers (search service), jsut for adding anotehr software? Or do you have a reason that you prefer not to state? – TomTom Mar 07 '11 at 07:55

2 Answers2

4

This is a nice introduction to lucene.Net:

It covers the basics of how to create an index, add documents to the index and finally how to search your index.

Midhat
  • 17,454
  • 22
  • 87
  • 114
Justin
  • 84,773
  • 49
  • 224
  • 367
0

1) Use this code to load all your file contents into a List():

var files = new List<string>();
foreach (var filePath in System.IO.Directory.EnumerateFiles("path_to_your_files")) {
    files.Add(System.IO.File.ReadAllText(filePath));
}

2) Read my article on how to setup basic Lucene.Net search:
Lucene.Net ultra fast search for MVC or WebForms site => made easy!

3) Instead of the static data in that article, use the data from your text files you got in step #1.

I hope that'll help!

mikhail-t
  • 4,103
  • 7
  • 36
  • 56