• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

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

OC/C/C++ 水墨上仙 1773次浏览

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
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C#目录内文件批量查找替换字符串内容
喜欢 (0)
加载中……