clsDirectoryScanner.cs

00001 // C# ClassLib
00002 // http://csharp-classlib.sourceforge.net
00003 //
00004 //
00005 // Copyright (C) 2005 Marcel Joachim Kloubert
00006 // 
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 // 
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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                 // The list of sub-directories that were found
00065                 private System.Collections.ArrayList m_oDirList;
00066                 // The list of files that were found
00067                 private System.Collections.ArrayList m_oFileList;
00068                 // Is this class instance scanning (true) or not
00069                 private System.Boolean m_boolBusy;
00070                 // The search pattern expression for files
00071                 private System.String m_strSearchPatternFiles;
00072                 // The search pattern expression for directories
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                                 // is busy => exit
00352                                 return;
00353 
00354                         // create temp lists for sub-directories & files if the scan process produces an exception
00355                         System.Collections.ArrayList oTMPDirs = new ArrayList();
00356                         System.Collections.ArrayList oTMPFiles = new ArrayList();
00357                 
00358                         // is busy
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                                 // process has finished
00369                                 this.m_boolBusy = false;
00370 
00371                                 if (null != this.OnScanComplete)
00372                                         // invoke the event(s)
00373                                         this.OnScanComplete(this, Directories, Files);
00374                         }
00375                         catch (System.Exception e)
00376                         {
00377                                 // first set busy state to false before throwing the exception
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                                 // Path does not exist!                         
00401                                 throw ( new System.IO.DirectoryNotFoundException("\"" + Path + "\" does not exist!") );
00402         
00403                         System.Int32 i = 0;
00404                         
00405                         // get dirs & files of Path
00406                         System.IO.DirectoryInfo oDirInfo = new System.IO.DirectoryInfo(Path);
00407                 
00408                         // first the sub-directories
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                                         // scan subdirs
00415                                         this.Scan(oSubDirs[i].FullName, Recursive, DirectoryList, FileList);
00416 
00417                                 if (null != this.OnDirectoryFound)
00418                                         // invoke the event(s)
00419                                         this.OnDirectoryFound(this, oSubDirs[i]);
00420                                 
00421                                 DirectoryList.Add(oSubDirs[i]);
00422                         }
00423                 
00424                         // now the files
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                                         // invoke the event(s)
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 }

Generated on Thu Jun 2 08:41:49 2005 for C# ClassLib by  doxygen 1.4.1