[C#] C#目录内文件批量查找替换字符串内容 →→→→→进入此内容的聊天室

来自 , 2019-05-23, 写在 C#, 查看 110 次.
URL http://www.code666.cn/view/3acb2a20
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.IO;
  7. public class MyClass
  8. {
  9.     public static string ReplaceText(string text, string search, string replace, string options)
  10.     {
  11.         RegexOptions ops = RegexOptions.None;
  12.         if (options == null)  //纯文本替换
  13.         {
  14.                         search = search.Replace(".", @".");
  15.                         search = search.Replace("?", @"?");
  16.                         search = search.Replace("*", @"*");
  17.                         search = search.Replace("(", @"(");
  18.                         search = search.Replace(")", @")");
  19.                         search = search.Replace("[", @"[");
  20.                         search = search.Replace("[", @"[");
  21.                         search = search.Replace("[", @"[");
  22.                         search = search.Replace("{", @"{");
  23.                         search = search.Replace("}", @"}");
  24.                         ops |= RegexOptions.IgnoreCase;
  25.         }
  26.         else
  27.                         if(options.Contains("I"))ops |= RegexOptions.IgnoreCase;
  28.         text = Regex.Replace(text, search, replace, ops);
  29.         return text;
  30.     }
  31.     public static bool ReplaceFile(string filename, string search, string replace,string options)
  32.     {
  33.         FileStream fs = File.OpenRead(filename);
  34.         //判断文件是文本文件还二进制文件。该方法似乎不科学
  35.         byte b;
  36.         for (long i = 0; i < fs.Length; i++)
  37.         {
  38.                         b = (byte)fs.ReadByte();
  39.                         if (b == 0)
  40.                         {
  41.                                 System.Windows.Forms.MessageBox.Show("非文本文件");
  42.                                 return false;//有此字节则表示改文件不是文本文件。就不用替换了
  43.                         }
  44.         }
  45.         //判断文本文件编码规则。
  46.         byte[] bytes = new byte[2];
  47.         Encoding coding=Encoding.Default;
  48.         if (fs.Read(bytes, 0, 2) > 2)
  49.         {
  50.                         if (bytes == new byte[2] { 0xFF, 0xFE }) coding = Encoding.Unicode;
  51.                         if (bytes == new byte[2] { 0xFE, 0xFF }) coding = Encoding.BigEndianUnicode;
  52.                         if (bytes == new byte[2] { 0xEF, 0xBB }) coding = Encoding.UTF8;
  53.         }
  54.         fs.Close();
  55.         //替换数据
  56.         string text=File.ReadAllText(filename, coding);
  57.         text=ReplaceText(text,search, replace, options);
  58.         File.WriteAllText(filename, text, coding);
  59.         return true;
  60.     }
  61.  
  62.         public static void RunSnippet()
  63.         {
  64.                 string path=@"D:\Desktop\Desktop\Asm";
  65.                 string options=null;
  66.                 if (Directory.Exists(path))
  67.         {
  68.                         foreach (string f in Directory.GetFiles(path))
  69.                         {
  70.                                 ReplaceFile(f, "        using", "@@@@@@@@", options);ReplaceFile(f, "using System.Threading;", "", options);
  71. ReplaceFile(f, "using System.Text;", "", options);
  72. ReplaceFile(f, "using System.Text.RegularExpressions;", "", options);
  73. ReplaceFile(f, "using System.Runtime.Serialization;", "", options);
  74. ReplaceFile(f, "using System.Runtime.InteropServices;", "", options);
  75. ReplaceFile(f, "using System.Runtime.CompilerServices;", "", options);
  76. ReplaceFile(f, "using System.Reflection;", "", options);
  77. ReplaceFile(f, "using System.Reflection.Emit;", "", options);
  78. ReplaceFile(f, "using System.Linq.Expressions;", "", options);
  79. ReplaceFile(f, "using System.IO;", "", options);
  80. ReplaceFile(f, "using System.Globalization;", "", options);
  81.                         }
  82.                         Console.Read();
  83.                         foreach (string f in Directory.GetFiles(path))
  84.                         {
  85. ReplaceFile(f, "using System.Diagnostics;", "", options);
  86. ReplaceFile(f, "using System.Collections;", "", options);
  87. ReplaceFile(f, "using System.Collections.Specialized;", "", options);
  88. ReplaceFile(f, "using System.Collections.Generic;", "", options);
  89. ReplaceFile(f, "using Rhino.Mocks.Utilities;", "", options);
  90. ReplaceFile(f, "using Rhino.Mocks.MethodRecorders;", "", options);
  91. ReplaceFile(f, "using Rhino.Mocks.Interfaces;", "", options);
  92. ReplaceFile(f, "using Rhino.Mocks.Impl;", "", options);
  93. ReplaceFile(f, "using Rhino.Mocks.Impl.RemotingMock;", "", options);
  94. ReplaceFile(f, "using Rhino.Mocks.Impl.InvocationSpecifications;", "", options);
  95.                         }
  96.                         Console.Read();
  97.                         foreach (string f in Directory.GetFiles(path))
  98.                         {
  99. ReplaceFile(f, "using System.ComponentModel;", "", options);
  100. ReplaceFile(f, "using Rhino.Mocks.Impl.Invocation;", "", options);
  101. ReplaceFile(f, "using Rhino.Mocks.Impl.Invocation.Specifications;", "", options);
  102. ReplaceFile(f, "using Rhino.Mocks.Impl.Invocation.Actions;", "", options);
  103. ReplaceFile(f, "using Rhino.Mocks.Generated;", "", options);
  104. ReplaceFile(f, "using Rhino.Mocks.Expectations;", "", options);
  105. ReplaceFile(f, "using Rhino.Mocks.Exceptions;", "", options);
  106. ReplaceFile(f, "using Rhino.Mocks.Constraints;", "", options);
  107. ReplaceFile(f, "using Castle.DynamicProxy;", "", options);
  108. ReplaceFile(f, "using Castle.Core.Interceptor;", "", options);
  109.                                 ReplaceFile(f, "@@@@@@@@", "    using", options);
  110.                         }/*
  111.                         Console.Read();
  112.                         foreach (string f in Directory.GetFiles(path))
  113.                         {
  114.                         }*/
  115.         }
  116.         }
  117.        
  118.         #region Helper methods
  119.        
  120.         public static void Main()
  121.         {
  122.                 try
  123.                 {
  124.                         RunSnippet();
  125.                 }
  126.                 catch (Exception e)
  127.                 {
  128.                         string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
  129.                         Console.WriteLine(error);
  130.                 }
  131.                 finally
  132.                 {
  133.                         Console.Write("Press any key to continue...");
  134.                         Console.ReadKey();
  135.                 }
  136.         }
  137.  
  138.         private static void WL(object text, params object[] args)
  139.         {
  140.                 Console.WriteLine(text.ToString(), args);      
  141.         }
  142.        
  143.         private static void RL()
  144.         {
  145.                 Console.ReadLine();    
  146.         }
  147.        
  148.         private static void Break()
  149.         {
  150.                 System.Diagnostics.Debugger.Break();
  151.         }
  152.  
  153.         #endregion
  154. }
  155. //csharp/1121

回复 "C#目录内文件批量查找替换字符串内容"

这儿你可以回复上面这条便签

captcha