1 /// <summary> 2 /// 替换文本文件中的词 3 /// </summary> 4 /// <param name="filePath"></param> 5 /// <param name="oldWord"></param> 6 /// <param name="newWord"></param> 7 public static void ReplaceWordInTxt(string fliePath, string oldWord, string newWord) 8 { 9 string target = File.ReadAllText(fliePath); 10 var strFile = Regex.Replace(target, @"b" + oldWord + @"b", newWord); 11 File.WriteAllText(fliePath, strFile); 12 }
参考:https://stackoverflow.com/questions/13544425/how-can-i-replace-a-word-in-a-txt-file-in-c-sharp