Using System; Using System.IO; Using System.Text.RegularExpressions; Class App {static void Main () {Console.WriteLine ("##########################" program uses the description "############################"); Console.WriteLine ("--Purpose: This program is used to delete the specified folder under the file containing the specified fields with--------------"); Console.WriteLine ("--The first step: Select the folder path to process (hint: paste through the upper left corner)---------"); Console.WriteLine ("--second step: Confirm whether to include subfolder file------------------------------------"); Console.WriteLine ("--Step Three: Enter the string to match the file name (if it matches, the file will be deleted)-------"); Console.WriteLine ("--Fourth step: Confirm delete operation, start running Program---------------------------"); Console.WriteLine ("######################################################################"); Console.WriteLine ("Please enter directory name:"); String Strdirectoryname=console.readline (); DirectoryInfo mydi=new DirectoryInfo (strdirectoryname); Deletes the file name that contains the specified field before Console.WriteLine ("contains subdirectories.") YES | NO: "); String Containchildren=console.readline (). Trim (); Console.WriteLine ("Please enter matching regular:"); String Regstr=console.readline (); Switch (Containchildren.tolower ()) {case "yes": Console.WriteLine ("Delete textFile list of files containing the specified field: "); Using (StreamWriter w = file.appendtext ("Log.txt")) {log ("before deleting" file name contains the list of files for the specified field: ", w);} Showdirectoryfilesalllevel (Mydi); Console.WriteLine (Confirm that you want to delete it.) YES | NO: "); if (Console.ReadLine (). ToLower () = = "Yes" {Console.WriteLine ("Delete file with file name matching +regstr+"); using (StreamWriter w = file.appendtext ("Log.txt") {Log ("delete" filename matches "+regstr+" File: ", W);} Deletefilesalllevel (MYDI,REGSTR); Deletes the file name that contains the specified field after Console.WriteLine ("Delete file name contains file list after files of specified field:"); Using (StreamWriter w = file.appendtext ("Log.txt")) {log (list of files under Deleted folder: ", W);} Showdirectoryfilesalllevel (Mydi); Console.WriteLine ("Execution completed.") Press any key to continue ... "); Console.read (); Using (StreamWriter w = file.appendtext ("Log.txt")) {log ("" Completed! "", W); else {break;} case "No": Console.WriteLine ("Delete file name contains the previous file list for the specified field:"); Showdirectoryfilesonelevel (Mydi); Console.WriteLine (Confirm that you want to delete it.) YES | NO: "); if (Console.ReadLine (). ToLower () = = "Yes" {Console.WriteLine ("Delete file with file name matching +regstr+"); using (StreamWriter w = file.appendtext ("Log.txt") ) {Log (before the delete file name contains the list of files for the specified field: ", w);} Deletefilesonelevel (MYDI,REGSTR); Deletes the file name that contains the specified field after Console.WriteLine ("Delete file name contains file list after files of specified field:"); Using (StreamWriter w = file.appendtext ("Log.txt")) {log (list of files under Deleted folder: ", W);} Showdirectoryfilesonelevel (Mydi); Using (StreamWriter w = file.appendtext ("Log.txt")) {log ("" Completed! "", W); } else {break;} default:Console.WriteLine ("Incorrect input, please enter a word: Yes or No.") "); Break //Gets the file private static void Deletefilesonelevel (DirectoryInfo dir,string regstr) {fileinfo[] Myfileinfo=dir under the current folder path. GetFiles (); foreach (FileInfo file in myfileinfo) {regex regex1=new regex (REGSTR); bool B=regex1. IsMatch (file. Name); if (b) {File.delete (File. FullName); Using (StreamWriter w = file.appendtext ("Log.txt")) {log ("Delete file:" +file.) Name, W); }}} private static void Showdirectoryfilesonelevel (DirectoryInfo dir) {fileinfo[] myfileinfo=dir. GetFiles (); if (myfileinfo.length==0) {Console.WriteLine ("There are no files in the current directory. "); Using (StreamWriter w = file.appendtext ("Log.txt")) {log ("No files in current directory! ", W); } else {foreach (FileInfo file in Myfileinfo) {Console.WriteLine ("" +file). Name); Using (StreamWriter w = file.appendtext ("Log.txt")) {log ("file name:" +file.) Name, W); The file private static void Deletefilesalllevel (DirectoryInfo dir,string regstr) {directoryinfo[] Mydirector Yinfo = dir. GetDirectories ()//Gets the name of the subdirectory fileinfo[] Myfileinfo = dir. GetFiles ()//Get file if (myfileinfo.length==0) {Console.WriteLine ("There is no file under the current directory.") "); Using (StreamWriter w = file.appendtext ("Log.txt")) {log ("No files in current directory!", w);}} else {foreach (FileInfo file in myfileinfo) {regex regex1=new regex (REGSTR); bool B=regex1. IsMatch (file. Name); if (b) {File.delete (File. FullName); Using (StreamWriter w = file.appendtext ("Log.txt")) {log ("Delete file:" +file.) Name, W); DirectoryInfo Dirinfo in Mydirectoryinfo) {Console.WriteLine ("folder:" +dirinfo.name); using (StreamWriter W = File.appendtext ("Log.txt")) {log ("file directory:" +dirinfo.fullname, W);} Deletefilesalllevel (DIRINFO,REGSTR); }} private static void Showdirectoryfilesalllevel (DirectoryInfo dir) {directoryinfo[] mydirectoryinfo = dir. GetDirectories ()//Gets the name of the subdirectory fileinfo[] Myfileinfo = dir. GetFiles ()//Gets the file foreach (FileInfo file in Myfileinfo) {Console.WriteLine ("" +file. Name); Using (StreamWriter w = file.appendtext ("Log.txt")) {log ("file name:" +file.) Name, W); } foreach (DirectoryInfo dirinfo in Mydirectoryinfo) {Console.WriteLine ("folder:" +dirinfo.name); using (StreamWriter w = Fi Le. AppendText ("Log.txt")) {log ("file directory:" +dirinfo.fullname, W);} Showdirectoryfilesalllevel (Dirinfo); } public static void Log (String logmessage, TextWriter W) {w.writeline ("{0} {1}", DateTime.Now.ToString (), LogMessage) ; } }