/// <summary>
/// 替换值
/// </summary>
/// <param name="strFilePath">txt等文件的路径</param>
/// <param name="strIndex">索引的字符串,定位到某一行</param>
/// <param name="newValue">替换新值</param>
private void ReplaceValue(string strFilePath, string strIndex, string newValue)
{
if (File.Exists(strFilePath))
{
string[] lines = System.IO.File.ReadAllLines(strFilePath);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains(strIndex))
{
string[] str = lines[i].Split('=');
str[1] = newValue;
lines[i] = str[0] + " = " + str[1];
}
}
File.WriteAllLines(strFilePath, lines);
}
}
C# 读取txt等文件中的内容,替换文本文件中的内容
最新推荐文章于 2023-09-22 11:55:36 发布
本文介绍了一种在指定文件中查找并替换特定字符串的方法。通过读取文件内容,搜索目标字符串,然后用新值进行替换,并将修改后的内容写回原文件。此方法适用于批量更新配置文件或其他文本文件。
767

被折叠的 条评论
为什么被折叠?



