using System.IO;
using System.Text;
/// <summary>
/// 打开文件并替换
/// </summary>
/// <param name="filename">文件名</param>
/// <param name="str">文件内容</param>
public void writefile(string filename,string str)
{
//StreamWriter SW;
Encoding code=Encoding.GetEncoding("utf-8");
string strpath=this.host_path;
strpath=Path.Combine(strpath,filename);
StreamWriter SW=new StreamWriter(strpath,false,code);
//SW=File.CreateText(strpath);
SW.Write(str);
SW.Close();
}
这是一个写页面的方法,由于.net每个页面有一个以.resx为扩展名的关联文件,
查看文件首行<?xml version="1.0" encoding="utf-8" ?>
我们可以看见该文件是编码为utf-8的xml文件,
如果要想让新写的页面代码生效必须符合这个编码格式
所以在写文件时添加Encoding code=Encoding.GetEncoding("utf-8");来设置文件流的编码
如果以上这些我写的有不明白的地方,欢迎留言共同探讨.