20 using Lucene.Net.Analysis.Standard;
21 using Lucene.Net.Index;
26 namespace Lucene.Net.Demo
30 public static class IndexFiles
32 internal static readonly DirectoryInfo INDEX_DIR =
new DirectoryInfo(
"index");
36 public static void Main(String[] args)
38 var usage = typeof(IndexFiles) +
" <root_directory>";
41 Console.Error.WriteLine(
"Usage: " + usage);
45 if (File.Exists(INDEX_DIR.FullName) ||
Directory.Exists(INDEX_DIR.FullName))
47 Console.Out.WriteLine(
"Cannot save index to '" + INDEX_DIR +
"' directory, please delete it first");
51 var docDir =
new DirectoryInfo(args[0]);
52 var docDirExists = File.Exists(docDir.FullName) ||
Directory.Exists(docDir.FullName);
55 Console.Out.WriteLine(
"Document directory '" + docDir.FullName +
"' does not exist or is not readable, please check the path");
59 var start = DateTime.Now;
64 Console.Out.WriteLine(
"Indexing to directory '" + INDEX_DIR +
"'...");
65 IndexDirectory(writer, docDir);
66 Console.Out.WriteLine(
"Optimizing...");
70 var end = DateTime.Now;
71 Console.Out.WriteLine(end.Millisecond - start.Millisecond +
" total milliseconds");
75 Console.Out.WriteLine(
" caught a " + e.GetType() +
"\n with message: " + e.Message);
79 internal static void IndexDirectory(
IndexWriter writer, DirectoryInfo directory)
81 foreach(var subDirectory
in directory.GetDirectories())
82 IndexDirectory(writer, subDirectory);
84 foreach (var file
in directory.GetFiles())
85 IndexDocs(writer, file);
88 internal static void IndexDocs(
IndexWriter writer, FileInfo file)
90 Console.Out.WriteLine(
"adding " + file);
96 catch (FileNotFoundException)
101 catch (UnauthorizedAccessException)