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