C#怎么实现一个目录下面所有后缀名相同的文件的关键字替换。怎么对文件先读后写。求代码  
例如在目录ttt下面替换所有后缀名为txt文件的很多行 strLine = strLine.Replace(@"./xcode.configVpp.pl -frmrate 5 -height" + height1 + "-width" + width1 + " -vidformat 1 -instance" + ins[z], @"./xcode.configVpp.pl -frmrate 5 -height" + height + "-width" + width + " -vidformat 1 -instance" + ins[z]);     width和height是变量 c#

解决方案 »

  1.   

    先获取文件列表,根据txt后缀判断文件,再获取文件内容到内存中,对内容按照你的规则过滤或替换后再写入文件并关闭文件,此流程操作结束
      

  2.   

    先获取txt文件列表,然后遍历,替换
      

  3.   

               
               List<string> ls = Directory.EnumerateFiles("文件目录", "*.txt").ToList();
                foreach (string s in ls)
                {
                    string st = File.ReadAllText(s, Encoding.Default);
                    st = st.Replace("旧值", "新值");
                    File.Delete(s);
                    File.WriteAllText(s,st,Encoding .Default);
                }