C#替换文件中特定字符串,按照原来的编码格式保存
2021-06-18 16:20
标签:[] https ring event lines cto 文件中 http 文件 C#替换文件中特定字符串,按照原来的编码格式保存 标签:[] https ring event lines cto 文件中 http 文件 原文地址:https://www.cnblogs.com/edwardorchis/p/10306729.htmlprivate void button1_Click(object sender, EventArgs e)
{
var txt1 = "E:\\Temp\\local";
string[] files = Directory.GetFiles(txt1, "*.txt");
foreach (string file in files)
{
execFile(file);
}
}
void execFile(string file)
{
int i = 0;
var eType = EncodingType.GetType(file);
var lines = File.ReadAllLines(file, eType);
StreamWriter sw = new StreamWriter(file, false, eType);
while(ilines.Length)
{
sw.WriteLine(lines[i].Replace("开始", "kaizhan"));
i++;
}
sw.Close();
}
EncodingType参考源:https://www.cnblogs.com/guyun/p/4262587.html