private void buttonOK5_Click(object sender, EventArgs e)  //删除
        {
            string str, All ;
            StreamReader stream1 = new StreamReader(@"f:\Test.txt", true);
            string str1 = this.textBName.Text;         
            while (str1.Length < 8)
                str1 = str1 + " ";
            if (str1.Length > 8)
                str1 = str1.Substring(0, 8);
            while ((str = stream1.ReadLine()) != null)
            {
                //str = stream1.ReadLine();
                if (str.Substring(0, 8) != str1)
                {
                    All += str;
                    MessageBox.Show(All);    //测试中数据进入了All;                }
                else if (str.Substring(0, 8) == str1)
                    MessageBox.Show("成功!");
            }
            stream1.Close();
            StreamWriter stream2 = new StreamWriter(@"f:\Test.txt");
            stream2.WriteLine(All);
            MessageBox.Show(All);   //可是在这里All就是空了!       
       }不知道是什么原因!
有谁能帮忙改改啊!
谢谢!!!!!!!!!!!

解决方案 »

  1.   

    StreamReader里面不是保存写入的吧
      

  2.   

    在最后执行stream1.Close(); 试下
      

  3.   

    建议设置断点调试一下,看ALL在哪边为空了
      

  4.   

    string str, All = "";
      

  5.   

                stream2.WriteLine(All); //这里有么?
                MessageBox.Show(All); 
      

  6.   

    是不是这里
    stream2.WriteLine(All); 
      

  7.   


    string str, All ;
                All = "";
                StreamReader stream1 = new StreamReader(@"f:\Test.txt", true); 
                string str1 = "gfdgfdgfdgdf";        
                while (str1.Length < 8) 
                    str1 = str1 + " "; 
                if (str1.Length > 8) 
                    str1 = str1.Substring(0, 8); 
                while ((str = stream1.ReadLine()) != null) 
                { 
                    //str = stream1.ReadLine(); 
                    if (str.Substring(0, 8) != str1)
                    {
                        All += str;
                        MessageBox.Show(All);
                    }//测试中数据进入了All;                } 
                    else
                    {
                        if (str.Substring(0, 8) == str1)
                            MessageBox.Show("成功!");
                    }
                } 
                stream1.Close(); 
                StreamWriter stream2 = new StreamWriter(@"f:\Test.txt"); 
                stream2.WriteLine(All); 
                MessageBox.Show(All);  //可是在这里All就是空了!      
     如果test.txt里面有内容的话,这段代码是没问题的.关键看你
     while ((str = stream1.ReadLine()) != null) 
    进去了没.
      

  8.   

    最后写入后没有stream2.Close();所以数据就丢了。
    所以在第一次运行时正常,第二次all就是空的了
      

  9.   

    string str="", All="" ; 
    StreamReader stream1 = new StreamReader(@"c:\CSDN.txt", true); 
    string str1 = "KevinTex";        
    // while (str1.Length < 8) 
    // str1 = str1 + " "; 
    // if (str1.Length > 8) 
    // str1 = str1.Substring(0, 8); 
    while ((str = stream1.ReadLine()) != null) 

    //str = stream1.ReadLine(); 
    if (str.Substring(0, 8) != str1) 

    All += str; 
    MessageBox.Show(All); 
    }//测试中数据进入了All;                
    else if (str.Substring(0, 8) == str1) 
    MessageBox.Show("成功!"); 
    }
    stream1.Close(); 
    StreamWriter stream2 = new StreamWriter(@"c:\CSDN.txt"); 
    stream2.WriteLine(All); 
    stream2.Close();
    MessageBox.Show(All);  //可是在这里All就是空了! 
      

  10.   

    这个是测试过的
    像soaringbird说的,一定要stream2.Close();
      

  11.   

    string str, All="" ; 
                StreamReader stream1 = new StreamReader(@"f:\Test.txt", true); 
                string str1 = "11111111";        
                while (str1.Length < 8) 
                    str1 = str1 + " "; 
                if (str1.Length > 8) 
                    str1 = str1.Substring(0, 8);             while ((str = stream1.ReadLine()) != null) 
                { 
                    //str = stream1.ReadLine(); 
                    if (str.Substring(0, 8) != str1) 
                    { 
                        All += str; 
                        MessageBox.Show(All);    //测试中数据进入了All;                
                    } 
                    else if (str.Substring(0, 8) == str1) 
                        MessageBox.Show("成功!"); 
                } 
                stream1.Close();             StreamWriter stream2 = new StreamWriter(@"f:\Test.txt"); 
                stream2.WriteLine(All); 
                MessageBox.Show(All);  //可是在这里All就是空了!     
                stream2.Close();
    经过测试可以删除
      

  12.   

    在最后加  stream2.Close();  All里是不为空了,不过不管我有多少的数据,就第一条记录回被保存!
    ?????
      

  13.   

    LZ,你那程序我测试了,如果
    if (str.Substring(0, 8) != str1) 

    All += str; 
    MessageBox.Show(All); 
    }//测试中数据进入了All;   
    程序能进入这里,最后哪个MessageBox.Show(All);  不可能显示不出来你是流没有关闭,导致数据丢失了.