DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory());
            string strall = "";
            foreach (FileInfo fa in di.GetFiles())
            {
                if (fa.Name == "SYSTEM.SYS")
                {
                    StreamReader sr = new StreamReader(fa.FullName);
                    strall = sr.ReadToEnd();
                    sr.Close();
                    StreamWriter sw = new StreamWriter(fa.FullName);
                    lin += lin + "\n";
                    if (lin.EndsWith("\n"))
                    {
                        string lst = strall.Replace(lin ,string.Empty);
                        sw.WriteLine(lst);
                    }
lin 不是固定的值,但是文件中的一部分字符
我想从文件中删除与lin相匹配的字符,但是我上面的方法一直不成功
  string lst = strall.Replace(lin ,"");我是想把replace掉,但是得到的结果lin还是存在而且字符还增加了
要怎么做?

解决方案 »

  1.   

    这里:lin += lin + "\n";
    你既然用了+=,那么后面就不要再出现lin了,“lin += "\n";”就可以了。
      

  2.   

    关键是replace后原来的没删掉,却还增加了一部分
      

  3.   

    你应该断点看看,strall中是否包含lin 的字符串,如果没有包含,即使Repalce也没用。
      

  4.   


     StreamWriter sw = new StreamWriter(fa.FullName,false);覆盖原有的文本,重新写入需要设置参数append=false
    是这个意思?
      

  5.   

    lin 是根据一定条件从文件中读出来的一部分,strall是整个文件读出来的值
    strall中一定包含lin 的字符串