1using System; 2using System.IO; 3using Lucene.Net.Analysis.Standard; 4using Lucene.Net.Index; 5using Lucene.Net.Util; 6using LuceneEngine.MakeFileDoc; 7 8namespace LuceneEngine.Index 9{ 10 public class IndexFiles 11 { 12 #region Properties ... 13 14 private string mstrFilesLocation = ""; 15 public string FilesLocation 16 { 17 get { return mstrFilesLocation; } 18 set { mstrFilesLocation = value; } 19 } 20 private string mstrIndexLocation = ""; 21 public string IndexLocation 22 { 23 get { return mstrIndexLocation; } 24 set { mstrIndexLocation = value; } 25 } 26 private string mstrError = ""; 27 public string Error 28 { 29 get { return mstrError; } 30 } 31 private long mlngNumDocsIndexed = 0; 32 public long NumDocsIndexed 33 { 34 get { return mlngNumDocsIndexed; } 35 set { mlngNumDocsIndexed = value; } 36 } 37 private long mlngNumDocsSkipped = 0; 38 public long NumDocsSkipped 39 { 40 get { return mlngNumDocsSkipped; } 41 set { mlngNumDocsSkipped = value; } 42 } 43 private long mlngNumDocsErrored = 0; 44 public long NumDocsErrored 45 { 46 get { return mlngNumDocsErrored; } 47 set { mlngNumDocsErrored = value; } 48 } 49 private long mlngTotalTime = 0; 50 public long TotalTime 51 { 52 get { return mlngTotalTime; } 53 set { mlngTotalTime = value; } 54 } 55 private bool mbStopProcessing = false; 56 public bool StopProcessing 57 { 58 set { mbStopProcessing = value; } 59 } 60 #endregion 61 #region Private Vars ... 62 63 string[] mstrSubDirectories = {null}; 64 #endregion 65 #region Custom Events ... 66 67 public delegate void OnPercentCompleteChangedHandler(object sender, System.EventArgs e, int intPC, long lngNumDocs, string strMessage); 68 public event OnPercentCompleteChangedHandler OnPercentCompleteChanged; 69 public delegate void OnErrorIndexingHandler(object sender, System.EventArgs e, string strError); 70 public event OnErrorIndexingHandler OnErrorIndexing; 71 #endregion 72 #region StartIndexing ... 73 74 public bool StartIndexing() 75 { 76 bool bResult = false; 77 try 78 { 79 //Validate folder locations 80 if (mstrFilesLocation.Length == 0) 81 { 82 mstrError = "Missing file/directory to index."; 83 } 84 else if (mstrIndexLocation.Length == 0) 85 { 86 mstrError = "Error: Missing index directory location."; 87 } 88 else 89 { 90 //Gather all subdirectories 91 System.EventArgs p = new System.EventArgs(); 92 OnPercentCompleteChanged (this, p, 0, 0, "Gathering list of directories to index..."); 93 AddSubDirectory(mstrFilesLocation); 94 GetDirectories(mstrFilesLocation); 95 96 //Start indexing 97 DateTime dteStart = DateTime.Now; 98 int intPercentComplete = 0; 99 IndexWriter objIndexWriter = new IndexWriter(mstrIndexLocation, new StandardAnalyzer(), true); 100 double j = mstrSubDirectories.GetUpperBound(0); 101 //Loop through all subdirectories 102 for (double i = 0; i <= j; i++) 103 { 104 //If "Stop" clicked, stop 105 if (mbStopProcessing) 106 break; 107 int i2 = Convert.ToInt32(i); 108 //Trigger event 109 OnPercentCompleteChanged (this, p, intPercentComplete, mlngNumDocsIndexed, "Indexing " + mstrSubDirectories[i2]); 110 //If directory, index directory 111 if (Directory.Exists(mstrSubDirectories[i2])) 112 IndexDocs(objIndexWriter, new DirectoryInfo(mstrSubDirectories[i2])); 113 else //index file 114 IndexDocs(objIndexWriter, new FileInfo(mstrSubDirectories[i2])); 115 //Update percent complete 116 int intPCTemp = 0; 117 if (j > 0) intPCTemp = Convert.ToInt32(i / j * 100); 118 if (intPercentComplete != intPCTemp) 119 { 120 intPercentComplete = intPCTemp; 121 } 122 } 123 //Finish up 124 OnPercentCompleteChanged (this, p, 100, mlngNumDocsIndexed, "Finishing up..."); 125 objIndexWriter.Optimize(); 126 objIndexWriter.Close(); 127 objIndexWriter = null; 128 DateTime dteEnd = DateTime.Now; 129 mlngTotalTime = (Date.GetTime(dteEnd) - Date.GetTime(dteStart)); 130 bResult = true; 131 } 132 } 133 catch (Exception e) 134 { 135 mstrError = "Exception: " + e.Message; 136 } 137 return bResult; 138 } 139 #endregion 140 #region IndexDocs ... 141 142 private void IndexDocs(IndexWriter objIndexWriter, FileSystemInfo objFile) 143 { 144 //If a directory, index all files (calls self) 145 if (objFile is DirectoryInfo) 146 { 147 FileInfo[] objFiles = (objFile as DirectoryInfo).GetFiles(); 148 for (int i = 0; i < objFiles.Length; i++) 149 IndexDocs(objIndexWriter, objFiles[i]); 150 } 151 else //it's a file, index it 152 { 153 try 154 { 155 string strFileName = objFile.ToString(); 156 int intPeriod = strFileName.LastIndexOf("."); 157 string strExtension = strFileName.Substring(intPeriod + 1).ToLower(); 158 //Only index files we can read as plain text (with a Reader) 159 //This code block will change as we create more indexers (for PDFs, etc.) 160 if (strExtension == "doc" || strExtension == "xls" || strExtension == "txt" || 161 strExtension == "asp" || strExtension == "aspx" || strExtension == "bat" || 162 strExtension == "ini" || strExtension == "sys" || strExtension == "sln" || 163 strExtension == "vbproj" || strExtension == "vb" || strExtension == "asmx" || 164 strExtension == "csproj" || strExtension == "cs" || strExtension == "htm" || 165 strExtension == "html" || strExtension == "asax" || strExtension == "css" || 166 strExtension == "sql" || strExtension == "js" || strExtension == "rtf" || 167 strExtension == "ascx" || strExtension == "wsdl" || strExtension == "config" || 168 strExtension == "vsd" || strExtension == "pdf") 169 { 170 objIndexWriter.AddDocument(FileDocument.Document(objFile as FileInfo)); 171 mlngNumDocsIndexed += 1; 172 } 173 else 174 { 175 mlngNumDocsSkipped += 1; 176 } 177 } 178 catch (Exception ex) 179 { 180 //AddMessage(objFile + " failed to index."); 181 mlngNumDocsErrored += 1; 182 System.EventArgs p = new System.EventArgs(); 183 OnErrorIndexing (this, p, "Exception while adding " + objFile + ": " + ex.Message); 184 } 185 } 186 } 187 #endregion 188 #region GetDirectories ... 189 190 public void GetDirectories(string strStartDir) 191 { 192 //Recurse into subdirectories of this directory. 193 string[] objSubDirs = Directory.GetDirectories(strStartDir); 194 foreach(string strDir in objSubDirs) 195 { 196 if (mstrIndexLocation != strDir) 197 { 198 AddSubDirectory(strDir); 199 } 200 //Do not iterate through reparse points 201 if ((File.GetAttributes(strDir) & FileAttributes.ReparsePoint) 202 != FileAttributes.ReparsePoint) 203 { 204 GetDirectories(strDir); 205 } 206 } 207 } 208 #endregion 209 #region AddSubDirectory ... 210 211 private void AddSubDirectory(string strDirectory) 212 { 213 //Add subdirectory to the array 214 if (mstrSubDirectories[0] == null) 215 { 216 mstrSubDirectories[0] = strDirectory; 217 } 218 else 219 { 220 mstrSubDirectories = ReDimArray(mstrSubDirectories, mstrSubDirectories.GetUpperBound(0) + 2); 221 mstrSubDirectories[mstrSubDirectories.GetUpperBound(0)] = strDirectory; 222 } 223 } 224 #endregion 225 #region ReDimArray ... 226 227 private static string[] ReDimArray(string[] OriginalArray, int DesiredSize) 228 { 229 string[] DestinationArray = new string[DesiredSize]; 230 Array.Copy(OriginalArray, DestinationArray, DesiredSize - 1); 231 return DestinationArray; 232 } 233 #endregion 234 } 235}