C # beginner training - Text Replacement Tool Commands

xiaoxiao2021-03-06  60

The program uses regular expressions to make text replacement, and the breadth priority is a lot of life (basic knowledge is important), solving problems that cannot be replaced back-wrap Class ReplaceE {////

/// replacement files in characters /// < / summary> /// file full name /// String /// String /// Whether backup file Private Static Void Doreplace (String Filefullname, String ReplaceDBY, String FindPattern, Bool IsBackup) {String result = string.Empty; string inputText = string.Empty; string replacement = replacedBy; string pat = findPattern; Regex r = new Regex (pat, RegexOptions.IgnoreCase); try {using (StreamReader sr = new StreamReader (fileFullName)) { INPUTTEXT = Sr.ReadToend ();} // compile the regular expression. if (isbackup == true) {try {file.copy (filefullname, filefullname ".bak"); } Catch (system.io.ioException ex) {file.copy (filefullname, filefullname ".bak", true); Console.writeLine (ex.Message);}} result = r.Replace (InputText, Replacement); // add some text to the file. Us (streamwriter sw = new streamwriter (filefullname) {sw.write (result); }}} Console.writeline;} catch (exception e) {console.writeline ("The Process Failed: {0}", E.TOSTRING ()); // Throw (e);}} /// < Summary> /// Trade Directory /// /// start path <

/ param> /// String /// String /// that the backup files /// whether to acquire subfolders public static void TravelReplace (string path, string replacedStr, string findPattern, bool isBackup, bool isGetSubFloder) {Queue queue = new Queue (); DirectoryInfo di = null; string subPath = string.Empty; string currentPath = string.Empty; FileSystemInfo [] dirs = null; queue.Enqueue (path); while (queue.Count > 0) {currentPath = (string) queue.Dequeue (); di = new DirectoryInfo (currentPath); // get files under current directiory FileSystemInfo [] files = di.GetFiles ( "* sql."); foreach (FileSystemInfo f in files) {doReplace (f.FullName, replacedStr, findPattern, isBackup);} // Get only subdirectories if (isGetSubFloder == true) {dirs = di.GetDirectories (); foreach (FileSystemInfo d in dirs) {subPath = d .Fullna Me; Queue.enqueue (Subpath); console.writeline (subpath);}}}}} test: Replacee.travelReplace (@ "E: / TEMP / TTT", "/ R / N);", @ "(/ @" R / n) {2,} /); "True, true);

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

New Post(0)