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 " ));
}
}
}
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 " ));
}
}
}