C#目录内文件批量查找替换字符串内容
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.IO; public class MyClass { public static string ReplaceText(string text, string search, string replace, string options) { RegexOptions ops = RegexOptions.None; if (options == null) //纯文本替换 { search = search.Replace(".", @"."); search = search.Replace("?", @"?"); search = search.Replace("*", @"*"); search = search.Replace("(", @"("); search = search.Replace(")", @")"); search = search.Replace("[", @"["); search = search.Replace("[", @"["); search = search.Replace("[", @"["); search = search.Replace("{", @"{"); search = search.Replace("}", @"}"); ops |= RegexOptions.IgnoreCase; } else if(options.Contains("I"))ops |= RegexOptions.IgnoreCase; text = Regex.Replace(text, search, replace, ops); return text; } public static bool ReplaceFile(string filename, string search, string replace,string options) { FileStream fs = File.OpenRead(filename); //判断文件是文本文件还二进制文件。该方法似乎不科学 byte b; for (long i = 0; i < fs.Length; i++) { b = (byte)fs.ReadByte(); if (b == 0) { System.Windows.Forms.MessageBox.Show("非文本文件"); return false;//有此字节则表示改文件不是文本文件。就不用替换了 } } //判断文本文件编码规则。 byte[] bytes = new byte[2]; Encoding coding=Encoding.Default; if (fs.Read(bytes, 0, 2) > 2) { if (bytes == new byte[2] { 0xFF, 0xFE }) coding = Encoding.Unicode; if (bytes == new byte[2] { 0xFE, 0xFF }) coding = Encoding.BigEndianUnicode; if (bytes == new byte[2] { 0xEF, 0xBB }) coding = Encoding.UTF8; } fs.Close(); //替换数据 string text=File.ReadAllText(filename, coding); text=ReplaceText(text,search, replace, options); File.WriteAllText(filename, text, coding); return true; } public static void RunSnippet() { string path=@"D:\Desktop\Desktop\Asm"; string options=null; if (Directory.Exists(path)) { foreach (string f in Directory.GetFiles(path)) { ReplaceFile(f, " using", "@@@@@@@@", options);ReplaceFile(f, "using System.Threading;", "", options); ReplaceFile(f, "using System.Text;", "", options); ReplaceFile(f, "using System.Text.RegularExpressions;", "", options); ReplaceFile(f, "using System.Runtime.Serialization;", "", options); ReplaceFile(f, "using System.Runtime.InteropServices;", "", options); ReplaceFile(f, "using System.Runtime.CompilerServices;", "", options); ReplaceFile(f, "using System.Reflection;", "", options); ReplaceFile(f, "using System.Reflection.Emit;", "", options); ReplaceFile(f, "using System.Linq.Expressions;", "", options); ReplaceFile(f, "using System.IO;", "", options); ReplaceFile(f, "using System.Globalization;", "", options); } Console.Read(); foreach (string f in Directory.GetFiles(path)) { ReplaceFile(f, "using System.Diagnostics;", "", options); ReplaceFile(f, "using System.Collections;", "", options); ReplaceFile(f, "using System.Collections.Specialized;", "", options); ReplaceFile(f, "using System.Collections.Generic;", "", options); ReplaceFile(f, "using Rhino.Mocks.Utilities;", "", options); ReplaceFile(f, "using Rhino.Mocks.MethodRecorders;", "", options); ReplaceFile(f, "using Rhino.Mocks.Interfaces;", "", options); ReplaceFile(f, "using Rhino.Mocks.Impl;", "", options); ReplaceFile(f, "using Rhino.Mocks.Impl.RemotingMock;", "", options); ReplaceFile(f, "using Rhino.Mocks.Impl.InvocationSpecifications;", "", options); } Console.Read(); foreach (string f in Directory.GetFiles(path)) { ReplaceFile(f, "using System.ComponentModel;", "", options); ReplaceFile(f, "using Rhino.Mocks.Impl.Invocation;", "", options); ReplaceFile(f, "using Rhino.Mocks.Impl.Invocation.Specifications;", "", options); ReplaceFile(f, "using Rhino.Mocks.Impl.Invocation.Actions;", "", options); ReplaceFile(f, "using Rhino.Mocks.Generated;", "", options); ReplaceFile(f, "using Rhino.Mocks.Expectations;", "", options); ReplaceFile(f, "using Rhino.Mocks.Exceptions;", "", options); ReplaceFile(f, "using Rhino.Mocks.Constraints;", "", options); ReplaceFile(f, "using Castle.DynamicProxy;", "", options); ReplaceFile(f, "using Castle.Core.Interceptor;", "", options); ReplaceFile(f, "@@@@@@@@", " using", options); }/* Console.Read(); foreach (string f in Directory.GetFiles(path)) { }*/ } } #region Helper methods public static void Main() { try { RunSnippet(); } catch (Exception e) { string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString()); Console.WriteLine(error); } finally { Console.Write("Press any key to continue..."); Console.ReadKey(); } } private static void WL(object text, params object[] args) { Console.WriteLine(text.ToString(), args); } private static void RL() { Console.ReadLine(); } private static void Break() { System.Diagnostics.Debugger.Break(); } #endregion }