.NETC # Package the tool class for disk directory file search function (implemented related events related to search, insert int

xiaoxiao2021-03-06  44

/ 9*.NET/C (Capacity) The tool class for the disk directory file search function (implements related events related to search so that the customer processing code) * /

// SearchwitHevents.cs: // Output EXE: // csc.exe searchwithevents.cs /r:c:/Windows/Microsoft.net/framework/viisualbasic.dll// output DLL: // CSC . e r s it. it it;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; / .NET / C # Implement the tool class (search event) /// public class search {///

//////// //// Searched source directory /// Source directory all subdirectory search mode /// search all files matching pattern source directory below /// the relative paths private void searching (string SourceDirectory, string DirectoryPatterns, string FilePatterns, string DestinationDirectory) {This._directoryPatterns = DirectoryPatterns; this._filepatterns = filepatterns;

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;}}

///

/// The information stored in the directory structure of the source directory path information relative /// public string CurrentDestinationDirectoryName {get {return _CurrentDestinationDirectoryName (_CurrentDestinationDirectoryName.EndsWith (@ "/") ""?: @ "/");}}

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 = @ "E: / TEMP / TEMP1 / New Folder";

x.searching (@ "E: / myc # /", "*", "* .cs; *. exe");

System.console.writeline ("Handling {0} Directory 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); / / According to the original directory of the search, at the specified create a new folder with the same name directory (copy the directory) 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 directorys, do not continue to find //sender.cancel = cancelateS.CRLDIRECTORE;} //sender.cancel = cancelateS.currentdirectory;

Private void x_searchedfile (Search Sender, SearcheventArgs 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 this new directory / / if // {//System.IO.Directory.CreateDirectory(e.CurrentDestinationDirectoryName); //} (System.IO.Directory.Exists (Sender.CurrentDestinationDirectoryName)!) //System.Console.WriteLine("{0} ", E.currentDestinationDirectoryName); //sender.cancel = cancelings.alldirector;

/ / 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 {// achieve * .exe file to the specified directory copy function 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 = cancelings.alldirectories;} //sender.cancel = cancelings.allDirector; System.console.writeline ("/ t {0}: {1}", E.FileUid, E.FileName);} private void fileprocess (String Source, String Destination) {// The implementation here is the pure The text of the text file is converted from Simplified Chinese to Traditional Chinese and saves 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.default); string s; while ((s = sr.readline ())! = null) {sw.writeline (Microsoft.visualBasic.strings.StrConv (s, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, System.Globalization.CultureInfo.CurrentCulture.LCID));} 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 / "列 印: Searched Directory"); if (System.Console.Readline (). TOLOWER () == "Y") {Foreach (String S In sender.searchddirector) {system.console.writeline (s); //sender.searchddirectories is readonly //sender.searchddirectories.add ("Kkk "); // If this sentence will run out of the exception}}}}}}}}}}}}}

转载请注明原文地址:https://www.9cbs.com/read-58196.html

New Post(0)