/*.NET/C # Tool class (search event) * /
// SearchwitHevents.cs: // Output EXE: // csc.exe searchwithevents.cs /r:c:/Windows/Microsoft.net/framework/viisualbasic.dll// output DLL: // CSC . EXE / T: library searchwithevents.cs /r:c:/Windows/MICROSOFT.NET/framework/v1.1.4322/Microsoft.visualbasic.dll
// Using system; namespace microshaoft.Utils {///
String [] patterns = this._directoryPatterns.split (';'); string [] patterns = this._filepatterns.split (';');
THIS._DIRECTORYID = 0; this._fileID = 0;
DestinationDirectory = (destinationDirectory.endswith (@ "/")? "": @ "/");
IF (this._directoriescount == 0) // Process the current file of the source directory {this._directoriescount ; this._directoryid ; this._currentdirectoryName = sourceDirectory;
IF (SOURCEDIRECTORY.ENDSWITH (@ "/")) {this._currentdirectoryName = SourceDirectory.Substring (0, SourceDirectory.length - 1);}
this._ParentDirectoryName = this._CurrentDirectoryName.Substring (this._CurrentDirectoryName.LastIndexOf (@ "/") 1); DestinationDirectory = this._ParentDirectoryName @ "/"; this._CurrentDestinationDirectoryName = DestinationDirectory; if (this.AddSearchedDirectory (this ._CurrentdirectoryName)) {this._directoryuid ;}
if (SearchedDirectory = null!) // trigger a directory to find the source of the event {OnSearchedDirectory (SourceDirectory, this._DirectoriesCount, this._DirectoryID, this._CurrentDestinationDirectoryName);} foreach (string p in patterns) {foreach (string f in System. IO.Directory.getfiles (SourceDirectory, P. Trim ())) {this._filescount ; this._fileid ;
IF (this.addsearchedfile (f)) {this._fileuid ;}
if (SearchedFile = null!) {OnSearchedFile (f, DirectoryUID, FileUID, this._DirectoryID, this.FileID, this._CurrentDestinationDirectoryName);} if (this._Cancel = CancelActions.No!) {break;}} if (this. _CANCEL! = CancelateS.no) {Break;}}}} f (this._cance! = CancelateS.AllDirector) {this._fileID = 0; this._directoryId = 0;
Foreach (String P in patterns) {if (this._cancel! = cancelactions.alldirector) {foreach (String D in system.io.directory.getdirector)) {if (this._cancel! = Cancelates.allDirectories) {this._directoriescount ; this._directoryID ;
THIS._CURRENTDIRECTORYNAME = D (D.Endswith (@ "/")? "": @ "/"); this._currentdestinationDirectoryName = destinationDirectory D.SUBSTRING (D.lastIndexof (@ "/") 1) @ "/"; IF (this.addsearchedDirectory (this._currentdirectoryname) {this._directoryuid ;}
if (SearchedDirectory = null!) {OnSearchedDirectory (d, DirectoryUID, this._DirectoryID, this._CurrentDestinationDirectoryName);} if (this._Cancel == CancelActions.CurrentDirectory) {this._Cancel = CancelActions.No; continue;} else if ( this._Cancel == CancelActions.AllDirectories) {break;}! if (this._Cancel = CancelActions.AllDirectories) foreach (string p in patterns) {foreach (string f in System.IO.Directory.GetFiles (d, p.Trim ())) {THIS._FILESCOUNT ; this._fileID ;
IF (this.addsearchedfile (f)) {this._fileuid ;}
if (SearchedFile = null!) {OnSearchedFile (f, DirectoryUID, FileUID, this._DirectoryID, this.FileID, this._CurrentDestinationDirectoryName);} if (this._Cancel = CancelActions.No!) {break;}} if (this. ! _Cancel = CancelActions.No) {break;}} if (this._Cancel == CancelActions.CurrentDirectory) {this._Cancel = CancelActions.No; continue;} else if (this._Cancel == CancelActions.AllDirectories) {break; } if (this._Cancel = CancelActions.AllDirectories!) {this._Nest ; Searching (d, this._DirectoryPatterns, this._FilePatterns, this._CurrentDestinationDirectoryName); this._Nest -;}}}} if (this. _CANCEL == CancelateS.currentDirectory) {this._cancel = can;} else} else} (this._cancel == cancela Ctions.allDirectories) {Break;}
}
IF ((this._nest == 0)) {if (Searched! = null) {Onserched (this.directoryUID, this.fileuid, this._currentdestinationDirectoryName);}}}
Public void search (STRING SOURCEDIRECTORY) {searching (SourceDirectory, "*", "*", this._destinationdirectory;
Public void search (STRING SOURCEDIRECTORY, STRING FilePatterns) {searching (SourceDirectory, "*", FilePatterns, this._destinationdirectory;
public void Searching (string SourceDirectory, string DirectoryPatterns, string FilePatterns) {Searching (SourceDirectory, DirectoryPatterns, FilePatterns, this._DestinationDirectory);} private void OnSearched (int DirectoryUID, int FileUID, string CurrentDestinationDirectoryName) {SearchEventArgs sea = new SearchEventArgs (DirectoryUID, FileUid, CurrentDestinationDirectoryName; Searched (this, sea);
private void OnSearchedFile (string f, int DirectoryUID, int FileUID, int DirectoryID, int FileID, string CurrentDestinationDirectoryName) {SearchEventArgs sea = new SearchEventArgs (f, DirectoryUID, FileUID, DirectoryID, FileID, CurrentDestinationDirectoryName); // new SearchEventHandler (SearchedFile). BeginInvoke (this, sea, new system.asynccallback (this.searchfilecallback), sea); SearchedFile (this, sea);}
Private Void SearchedFileCallback (System.iasyncResult IAR) {throw new system.notimplementException ();}
Private Void SearchedDirectoryCallback (System.iasyncResult IAR) {throw new system.notimplementException ();
private void OnSearchedDirectory (string d, int DirectoryUID, int DirectoryID, string CurrentDestinationDirectoryName) {SearchEventArgs sea = new SearchEventArgs (d, DirectoryUID, DirectoryID, CurrentDestinationDirectoryName); // new SearchEventHandler (SearchedDirectory) .BeginInvoke (this, sea, new System.AsyncCallback (SearchedDirectoryCallback, SEA); SearchedDirectory (this, sea);
Public Delegate Void SearcheventHandler (Search Sender, Searcheventargs E);
private int _Nest; // recursive nesting public event SearchEventHandler SearchedDirectory; // "to search a directory" event public event SearchEventHandler SearchedFile; // "to search for a file," the event public event SearchEventHandler Searched; // "Search" event private int _directoriescount; // Search for the number of times private int _filescount; // Search files
Private string _filepatterns = "*"; // File name matching mode private string _directoryPatterns = "*"; // directory name match mode private cancelactions _cancel; // cancel
Private string _currentdirectoryName; // Search for the current directory name private string _filename = null; private int _fileId; // Search for ID private int _directoryId; // Search for the current directory's parent directory ID Private string _CurrentDestinationDirectoryName; // Store relative path directories, can be due to copy directories
Private string_destinationdirectory; private string _parentdirectoryName
Private int _fileuid; // This search "" Private INT _DirectoryUID; // This search "Directory" private system.collections.arraylist _searchedDirectories; // stores the searched directory private system. Collects.Arraylist_SearchedFiles; // Store searched files
public System.Collections.ArrayList SearchedDirectories {get {// SearchedDirectories is ReadOnly return System.Collections.ArrayList.ReadOnly (this._SearchedDirectories);}} public System.Collections.ArrayList SearchedFiles {get {// SearchedFiles is ReadOnly return System.Collections .Arraylist.readonly (this._searchedfiles);}}
Public int Directoriescount {get {return _directoriescount;}}
Public int filescount {get {return_filescount;}}
Public String DirectoriesPatterns {get {return _directorypatterns;} set {_directorypatterns = value;}}
public string DestinationDirectory {get {return _DestinationDirectory;} set {_DestinationDirectory = value;}} public string CurrentDirectoryName {get {return _CurrentDirectoryName (_CurrentDirectoryName.EndsWith (@ "/") "?": @ "/");} set { _CurrentDirectoryName = value;}}
Public string filename {get {return _filename;}}
Public String ParentdirectoryName {get {return_parentdirectoryName;}}
///
Public int fileid {get {return _fileid;}}
Public int directoryId {get {return _directoryId;}}
Public CanceLActions Cancel {Get {Return_Cancel;} set {_cancel = value;}}
Public int directoryUid {get {return _directoryUId;}}
Public int fileuid {get {return _fileuid;}}
Public string filespatterns {get {return _filepatterns;} set {_filepatterns = value;}}
private bool AddSearchedDirectory (string Key) {if (this._SearchedDirectories == null) {this._SearchedDirectories = new System.Collections.ArrayList ();} bool b = this._SearchedDirectories.Contains (Key); if (b!) { THIS._SEARCHEDDIRECTORIES.ADD (key);} return! b;
private bool AddSearchedFile (string Key) {if (this._SearchedFiles == null) {this._SearchedFiles = new System.Collections.ArrayList ();} bool b = this._SearchedFiles.Contains (Key); if (b!) { THIS._SEARCHEDFILES.ADD (KEY);} Return! B;}}
Public Enum CanceLActions {No // Does not cancel, continue, currentdirectory // only cancel the current directory, allDirectories // Cancel all search}
public class SearchEventArgs: System.EventArgs {private int _FileID; private int _DirectoryID; private string _CurrentDirectoryName; private string _CurrentDestinationDirectoryName; private string _FileName; private int _DirectoriesCount = 0; private int _FileUID; private int _DirectoryUID; private int _FilesCount = 0;
Public int filescount {get {return_filescount;}}
Public int Directoriescount {get {return _directoriescount;}}
Public string currentdirectoryName {get {return _currentdirectoryName (_currentdirectoryname.endswith (@ "/")? "": @ "/");}}
Public string filename {get {return _filename;}}
Public String ParentdirectoryName {get {return_currentdirectoryname.substring (_CurrentDirectoryName.lastIndexof (@ "/") 1);}}
Public String CurrentDestinationDirectoryName {Get {Return_CurrentDestinationDirectoryName (_CurrentDestinationDirectoryName.endswith (@ "/")? "": @ "/");}}
Public int fileuid {get {return _fileuid;}}
Public int directoryUid {get {return _directoryUId;}}
Public int fileid {get {return _fileid;}}
Public int directoryId {get {return _directoryId;}}
internal SearchEventArgs (int DirectoryUID, int FileUID, string CurrentDestinationDirectoryName) {this._FileUID = FileUID; this._DirectoryUID = DirectoryUID; this._CurrentDestinationDirectoryName = CurrentDestinationDirectoryName;} internal SearchEventArgs (string FileName, int DirectoryUID, int FileUID, int DirectoryID, int FileID, string CurrentDestinationDirectoryName) {this._FileName = System.IO.Path.GetFileName (FileName); this._CurrentDirectoryName = System.IO.Path.GetDirectoryName (FileName); this._FileUID = FileUID; this._DirectoryUID = DirectoryUID; this._DirectoryID = DirectoryID; this._fileid = fileId; this._currentdestinationDirectoryName = currentdestinationDirectoryName;}
internal SearchEventArgs (string DirectoryName, int DirectoryUID, int DirectoryID, string CurrentDestinationDirectoryName) {this._CurrentDirectoryName = DirectoryName; this._DirectoryUID = DirectoryUID; this._DirectoryID = DirectoryID; this._CurrentDestinationDirectoryName = CurrentDestinationDirectoryName;}}}
// The following is the test program namespace test {using microshaoft.utils; public class apptest {private static void main () {search x = new search (); apptest a = new apptest (); // subscription "Search a directory "the event x.SearchedDirectory = new Search.SearchEventHandler (a.x_SearchedDirectory); // subscribe" to search for a file, "the event x.SearchedFile = new Search.SearchEventHandler (a.x_SearchedFile); // Subscribe" Search Event X.Searched = new search.searcheventhandler (a.x_search);
// Specify the target directory x.DestinationDirectory = @ "d: / temp /";
// Source path //x.searching (@"e:/kkk/ "," * "," * .aspx "); x.searching (@" c: /inetpub/wwroot/mobile/1.2/zhxqc/ " , "*", "*. cs; *." system.console.writeline ("Processes {1} files in directory!", x.directoryUid, x.fileuid; system .Console.readline ();
private void x_SearchedDirectory (Search Sender, SearchEventArgs e) {System.Console.WriteLine ( "{0}: / n {1} / n {2}", Sender.DirectoryUID, Sender.CurrentDirectoryName, e.CurrentDestinationDirectoryName); // The searched the original directory, create a new directory with the same name in the specified folder (directory copy) if (! System.IO.Directory.Exists (Sender.CurrentDestinationDirectoryName)) {//System.IO.Directory.CreateDirectory(e.CurrentDestinationDirectoryName) } If (sender.directoriescount> 5) // The event will be triggered 6 times {// Find 6 directory does not continue to find //sender.cancel = cancelateS.AllDirector;} //sender.cancel = cancelingss. Currentdirectory;
Private void x_searchedfile (search sender, searchargs e) {if (e.fileid == 1) // If you find the first file in a directory to create this new directory {// If you find the first file to create the new directory IF (System.IO.Directory.Exists (Sender.CurrentDestinationDirectoryName)!) {System.IO.Directory.CreateDirectory (e.CurrentDestinationDirectoryName);} //System.Console.WriteLine("{0} ", e.CurrentDestinationDirectoryName); / / Serner.cancel = Cancelate;
/ / Handling Search // In this handler, you can realize a full text search keyword (such as illegal remarkable speech) if you have illegal remarks can be processed. = ".exe") {// This is implemented here that the text of the search for plain text file * .cs is converted from the Simplified Chinese to Traditional Chinese and save the specified directory FileProcess (Sender.currentDirectoryName E.FileName, E .CurrentDestinationDirectoryName e.FileName);} else {// copy * .exe file to implement the function specified directory //System.IO.File.Copy(Sender.CurrentDirectoryName e.FileName, e.CurrentDestinationDirectoryName e.FileName, True);} If (sender.filescount> 100) // This event will be triggered 101 times {// find 101 files, do not continue to find //sender.cancel = cancelateS.AllDirectories;} //sender.cancel = Cancelate; "/ t {0}: {1}", E.FileUid, E.FileName);
Private Void FileProcess (String Source, String Destination) {// This is implemented here that the text of the plain text file is converted from the Simplified Chinese to Traditional Chinese and save the function of the specified directory System.io.StreamReader SR = New System. IO.StreamReader (Source, System.Text.Encoding.Default); //System.IO.StreamWriter sw = new System.IO.StreamWriter (Destination, false, System.Text.Encoding.GetEncoding ( "big5")); System .Io.streamwriter sw = new system.io.streamwriter (destination, false, system.text.encoding.uitode); string s;
S = Sr.ReadToeend (); sw.write (tcscconvert (s)); // while ((s = sr.readline ())! = null) // {///sw.writeline(microsoft.visualbasic. Strings.StrConv (s, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, System.Globalization.CultureInfo.CurrentCulture.LCID)); // sw.WriteLine (TCSCConvert (s)); //} sr.Close (); sr = null Sw.close (); SW = null;}
Private void x_searched (Search Sender, SearcheventArgs E) {system.console.writeline ("Finished Processing {0} Sub-Directory, {1} subfile!", sender.directoriescount, sender.filescount); System.Console.writeline "Press /" Y / "Normal Print: Searched Directory"); if (System.Console.Readline (). TOLOWER () == "Y") {FOREACH (String s in sender.searchdirector) {system. Console.writeline (s); //sender.searchddirectories is readonly //sender.searchddirectories.add ("Kkk "); // If this sentence will run out}}} private static word.documentclass_d; private static string TCSCCONVERT (STRINGS> 0) {if (_d == null) {_d = new word.documentclass ();} _d.content.text = S; _d.content.tcscconverter (Word.WDTCSCCONVERTERDIRECTION .wdtcscconvertC, true, true); s = _d.content.text;} Return S;}}
}