File.ReadAllText (String) 打开一个文本文件,读取文件的所有行,然后关闭该文件。
File.ReadAllText (String, Encoding) 打开一个文件,使用指定的编码读取文件的所有行,然后关闭该文件

File.WriteAllText (String, String) 创建一个新文件,在其中写入指定的字符串数组,然后关闭该文件。如果目标文件已存在,则改写该文件。
File.WriteAllText (String, String, Encoding) 创建一个新文件,使用指定的编码在其中写入指定的字符串数组,然后关闭文件。如果目标文件已存在,则改写该文件。
例子:
using System;
using System.IO;
using System.Text;
class Test
{
    
public static void Main()
    {
        
string path = @"D:\MyTest.txt";
        
string oldString="hello";
        
string newString="你好";
        
if (File.Exists(path))
        {
            
string content = File.ReadAllText(path , Encoding.GetEncoding("gb2312"));
            content 
= content.Replace(oldString, newString);
            File.WriteAllText(path , content, Encoding.GetEncoding(
"gb2312"));
       }
    }
}


相关文章:

  • 2021-12-09
  • 2021-12-26
  • 2021-08-04
  • 2021-10-17
  • 2022-03-13
  • 2022-12-23
  • 2021-12-30
  • 2022-02-08
猜你喜欢
  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2021-12-13
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案