已经知道文本中第几行,想用streamwriter把这行删除掉 不知道如何实现可以说是把这行删除掉 不留空格 不留空行  完全删除掉。请教

解决方案 »

  1.   

    ReadLine()一行一行读,然后跟过那一行,再写回去
      

  2.   

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;namespace CA1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string file = "sample.txt";//文件名
                string filecontent = string.Empty;
                int line = 2;//不需要的行
                int curline = 1;
                using (StreamReader sr = new StreamReader(file, Encoding.GetEncoding("GB2312")))
                {
                    while (sr.Peek() != -1)
                    {
                        string tmp = sr.ReadLine();
                        if (line != curline)
                        {
                            if (filecontent != string.Empty) filecontent += "\r\n";
                            filecontent += tmp;
                        }
                        curline++;
                    }
                }
                //写回
                using (StreamWriter sw = new StreamWriter(file, false, Encoding.GetEncoding("GB2312")))
                {
                    sw.Write(filecontent);
                }
                Console.ReadLine();
            }
        }
    }
      

  3.   

                StreamReader sd = File.OpenText("1.txt");
                string savestr= sd.ReadToEnd();
                sd.Close();
                int foundS1 = name.IndexOf(" ");
                int foundS2 = name.IndexOf(" ", foundS1 + 1);            if (foundS1 != foundS2 && foundS1 >= 0)
                {                name = name.Remove(foundS1 + 1, foundS2 - foundS1);
                    
                }            StreamWriter sw = File.CreateText("1.txt");
                sw.WriteLine(savestr);
                sw.Close();这是删除空格操作 自己写的 很容易看懂