各位老大,我想写一段程序,能够把一个web.config文件打开,然后改变其中一些内容,再写成另外一个文件,可是我写完的那个文件不知道为什么,好像是编码的问题,VS2003总是提示我文件格式有问题。
但我把它打开,做一个不影响内容的修改(比如,删除一个字母a,再在原来的位置写一个字母a),然后保存,就能正常运行,有没有人能够告诉我为什么啊?应该怎么调整一下阿
下面是这部分的代码:
StreamReader reader = new StreamReader(StrSourcePath + "\\" + FileName, System.Text.Encoding.GetEncoding("GB2312"));

string StrTargetFile = StrTargetFilePath + "\\" + FileName.Replace(".sre", "").Replace(Dir + "\\", "");
Fs = new FileStream(StrTargetFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);

writer = new StreamWriter(Fs, System.Text.Encoding.GetEncoding("GB2312"));//, true);

for(string line = reader.ReadLine(); line != null; line = reader.ReadLine())
{

writer.WriteLine(line);
}

解决方案 »

  1.   

    change
    System.Text.Encoding.GetEncoding("GB2312")with 
    System.Text.Encoding.Default
      

  2.   

    使用System.Text.Encoding.Default还不行?
    看看你的WEB。CONFIG头
    <?xml version="1.0" encoding="utf-8" ?>
      

  3.   

    但我把它打开,做一个不影响内容的修改?用什么打开?如果是记事本看看记事本的默认编码,建议使用 Unicode 除了文件大点外,因该都能兼容(并且自动转换Text ANSI 等内码)。
      

  4.   

    web.config 的头一定是
    <?xml version="1.0" encoding="utf-8" ?>还有,我是用VS2003打开的,因为最后我是生成了一个asp.net的项目
      

  5.   

    为什么不使用xml相关的reader以及writer呢参看
    http://qilusoft.org/QTEC/forums/855/ShowPost.aspx
    http://book.chinaz.com/CSDN/asppost9/web200396.htm