00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 using System;
00023 using System.Collections;
00024 using System.IO;
00025
00026 namespace CSharp_ClassLib.Filesystem
00027 {
00062 public class DirectoryScanner : System.Object
00063 {
00064
00065 private System.Collections.ArrayList m_oDirList;
00066
00067 private System.Collections.ArrayList m_oFileList;
00068
00069 private System.Boolean m_boolBusy;
00070
00071 private System.String m_strSearchPatternFiles;
00072
00073 private System.String m_strSearchPatternDirectories;
00074
00076 public delegate void ScanCompleteEventHandler (object sender, System.IO.DirectoryInfo[] DirectoryList, System.IO.FileInfo[] FileList);
00078 public delegate void DirectoryFoundEventHandler (object sender, System.IO.DirectoryInfo directory);
00080 public delegate void FileFoundEventHandler (object sender, System.IO.FileInfo file);
00081
00083 public event ScanCompleteEventHandler OnScanComplete;
00085 public event DirectoryFoundEventHandler OnDirectoryFound;
00087 public event FileFoundEventHandler OnFileFound;
00088
00101 public DirectoryScanner()
00102 {
00103 this.Clear();
00104
00105 this.m_boolBusy = false;
00106 this.SearchPatternFiles = "";
00107 this.SearchPatternDirectories = "";
00108
00109 return;
00110 }
00111
00129 public System.String SearchPatternFiles
00130 {
00131 get { return (("" == this.m_strSearchPatternFiles.Trim()) ? "*" : this.m_strSearchPatternFiles); }
00132 set { this.m_strSearchPatternFiles = value; }
00133 }
00134
00152 public System.String SearchPatternDirectories
00153 {
00154 get { return (("" == this.m_strSearchPatternDirectories.Trim()) ? "*" : this.m_strSearchPatternDirectories); }
00155 set { this.m_strSearchPatternDirectories = value; }
00156 }
00157
00175 public System.Boolean Busy { get { return this.m_boolBusy; } }
00176
00194 public System.IO.DirectoryInfo[] Directories
00195 {
00196 get
00197 {
00198 System.IO.DirectoryInfo[] oBuffer = new System.IO.DirectoryInfo[this.m_oDirList.Count];
00199 this.m_oDirList.CopyTo(oBuffer);
00200
00201 return oBuffer;
00202 }
00203 }
00204
00222 public System.IO.FileInfo[] Files
00223 {
00224 get
00225 {
00226 System.IO.FileInfo[] oBuffer = new System.IO.FileInfo[this.m_oFileList.Count];
00227 this.m_oFileList.CopyTo(oBuffer);
00228
00229 return oBuffer;
00230 }
00231 }
00232
00266 public void Start ()
00267 {
00268 this.Start(false, System.Environment.CurrentDirectory);
00269
00270 return;
00271 }
00272
00307 public void Start (System.Boolean Recursive)
00308 {
00309 this.Start(Recursive, System.Environment.CurrentDirectory);
00310
00311 return;
00312 }
00313
00348 public void Start (System.Boolean Recursive, System.String Path)
00349 {
00350 if (this.Busy)
00351
00352 return;
00353
00354
00355 System.Collections.ArrayList oTMPDirs = new ArrayList();
00356 System.Collections.ArrayList oTMPFiles = new ArrayList();
00357
00358
00359 this.m_boolBusy = true;
00360
00361 try
00362 {
00363 this.Scan(Path, Recursive, oTMPDirs, oTMPFiles);
00364
00365 this.m_oDirList = oTMPDirs;
00366 this.m_oFileList = oTMPFiles;
00367
00368
00369 this.m_boolBusy = false;
00370
00371 if (null != this.OnScanComplete)
00372
00373 this.OnScanComplete(this, Directories, Files);
00374 }
00375 catch (System.Exception e)
00376 {
00377
00378 m_boolBusy = false;
00379
00380 throw (e);
00381 }
00382
00383 return;
00384 }
00385
00397 private void Scan (System.String Path, System.Boolean Recursive, System.Collections.ArrayList DirectoryList, System.Collections.ArrayList FileList)
00398 {
00399 if (!System.IO.Directory.Exists(Path))
00400
00401 throw ( new System.IO.DirectoryNotFoundException("\"" + Path + "\" does not exist!") );
00402
00403 System.Int32 i = 0;
00404
00405
00406 System.IO.DirectoryInfo oDirInfo = new System.IO.DirectoryInfo(Path);
00407
00408
00409 System.IO.DirectoryInfo[] oSubDirs;
00410 oSubDirs = oDirInfo.GetDirectories( this.SearchPatternDirectories );
00411 for (i = 0; i < oSubDirs.GetLength(0); i++)
00412 {
00413 if (Recursive)
00414
00415 this.Scan(oSubDirs[i].FullName, Recursive, DirectoryList, FileList);
00416
00417 if (null != this.OnDirectoryFound)
00418
00419 this.OnDirectoryFound(this, oSubDirs[i]);
00420
00421 DirectoryList.Add(oSubDirs[i]);
00422 }
00423
00424
00425 System.IO.FileInfo[] oFiles;
00426 oFiles = oDirInfo.GetFiles( this.SearchPatternFiles );
00427 for (i = 0; i < oFiles.GetLength(0); i++)
00428 {
00429 if (null != this.OnFileFound)
00430
00431 this.OnFileFound(this, oFiles[i]);
00432
00433 FileList.Add(oFiles[i]);
00434 }
00435
00436 return;
00437 }
00438
00443 public void Clear ()
00444 {
00445 this.m_oDirList = new System.Collections.ArrayList();
00446 this.m_oFileList = new System.Collections.ArrayList();
00447
00448 return;
00449 }
00450 }
00451 }