using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.IO; namespace replace ... { class Program ...{ static void Main(string[] args) ...{ if (args.Length == 0||(args.Length == 1 && (args[0] == "/?" || args[0] == "?" || args[0] == "-?"))) ...{ Console.WriteLine("replace 命令行工具"); Console.WriteLine("作者:zj53hao@qq.com"); Console.WriteLine("CSDN:http://blog.csdn.net/zj53hao"); Console.WriteLine("用途:在很多需要动态更改某些软件的配置文件时非常有用"); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("replace 命令使用语法说明"); Console.WriteLine(" replace [/?|?|-?] 显示帮助信息"); Console.WriteLine(" replace [path/]filename search replace [/option] 替换文本文件中的字符串,支持通配符文件名"); Console.WriteLine(" replace setname search replace [/option] 替换参数变量中的字符串"); Console.WriteLine("/option参数说明:"); Console.WriteLine(" i 区分大小写. 默认不区分"); Console.WriteLine(" r 使用正则表达式查找文本。留空则使用纯文本替换"); Console.WriteLine(" f 替换文件 默认"); Console.WriteLine(" v 替换的是系统环境变量.和f不能同时出现"); Console.WriteLine(); Console.WriteLine("如: replace *.txt 查找字符串 替换字符串"); Console.WriteLine("如: replace setname 查找字符串 替换字符串"); Console.WriteLine("如: replace myinc/*.txt 查找字符串 替换字符串 /i"); Console.WriteLine("如: replace setname 查找字符串 替换字符串 /irv"); } if (args.Length == 3) ...{ ReplaceFiles(args[0],args[1],args[2],null); } if (args.Length == 4) ...{ if (args[3].Contains("v")) ...{ ReplaceVariable(args[0], args[1], args[2], args[3]); } else ...{ ReplaceFiles(args[0], args[1], args[2], args[3]); } } } /**//// <summary> /// 替换环境变量中某个变量的文本值。可以是系统变量,用户变量,临时变量。替换时会覆盖原始值。小心使用 /// </summary> /// <param name="variable"></param> /// <param name="search"></param> /// <param name="replace"></param> /// <param name="options"></param> public static void ReplaceVariable(string variable, string search, string replace, string options) ...{ string text=Environment.GetEnvironmentVariable(variable); System.Windows.Forms.MessageBox.Show(text); text=ReplaceText(text, search, replace, options); Environment.SetEnvironmentVariable(variable, text); text = Environment.GetEnvironmentVariable(variable); System.Windows.Forms.MessageBox.Show(text); } /**//// <summary> /// 批量替换文件文本 /// </summary> /// <param name="args"></param> public static void ReplaceFiles(string path,string search, string replace, string options) ...{ string[] fs; if(File.Exists(path))...{ ReplaceFile(path, search, replace, options); return; } if (Directory.Exists(path)) ...{ fs = Directory.GetFiles(path); foreach (string f in fs) ...{ ReplaceFile(f, search, replace, options); } return; } int i=path.LastIndexOf("/"); if(i<0)i=path.LastIndexOf("/"); string d, searchfile; if (i > -1) ...{ d = path.Substring(0, i + 1); searchfile = path.Substring(d.Length); } else ...{ d = System.Environment.CurrentDirectory; searchfile = path; } searchfile = searchfile.Replace(".", @"."); searchfile = searchfile.Replace("?", @"[^.]?"); searchfile = searchfile.Replace("*", @"[^.]*"); //System.Windows.Forms.MessageBox.Show(d); System.Windows.Forms.MessageBox.Show(searchfile); if (!Directory.Exists(d)) return; fs = Directory.GetFiles(d); foreach (string f in fs) ...{ if(System.Text.RegularExpressions.Regex.IsMatch(f,searchfile)) ReplaceFile(f, search, replace, options); } } /**//// <summary> /// 替换单个文本文件中的文本 /// </summary> /// <param name="filename"></param> /// <param name="search"></param> /// <param name="replace"></param> /// <param name="options"></param> /// <returns></returns> 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; } /**//// <summary> /// 替换文本 /// </summary> /// <param name="text"></param> /// <param name="search"></param> /// <param name="replace"></param> /// <param name="options"></param> /// <returns></returns> 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; } }}