小寒的blog
Programming is a darned hard thing—but I am going to like it.
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"));
       }
    }
}


posted on 2008-01-04 10:10  xhan  阅读(1706)  评论(0编辑  收藏  举报