做备忘录(前台删除功能) 数组列表

        private void btn_delete_Click(object sender, EventArgs e)
        {
            if (this.listBox1.Items.Count == 0)
            {
                MessageBox.Show("对不起,您的备忘录中没有要删除的数据!","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
            else

            {
                FileStream FStream = new FileStream(@"F:\ASP.NET\备忘录\a.txt",FileMode.Open);
                StreamReader SReader = new StreamReader(FStream,Encoding.Default);
                string strLine = SReader.ReadLine();
                List<string> list = new List<string>();
                while(strLine!=null)
                {
                    if (strLine != null && !strLine.Equals(""))
                    {
                        string[] read = strLine.Split(' ');
                        string a = this.listBox1.Items[this.listBox1.SelectedIndex].ToString();
                        string[] str = a.Split(' ');
                        if (str[0] != read[0].Trim() || str[1] != read[1].Trim() || str[2] != read[2].Trim()||str[3]!=read[3].Trim()||str[4]!=read[4].Trim())
                        {
                            list.Add(strLine);
                        }
                       
                   }
                 
                   strLine = SReader.ReadLine();
               }               
               SReader.Close();
               FStream.Close();
               FileInfo info = new FileInfo(@"F:\ASP.NET\备忘录\a.txt");
               info.Delete();    //删除源文件
              
               FileStream FStream1 = new FileStream(@"F:\ASP.NET\备忘录\a.txt", FileMode.Append);
               StreamWriter StrWriter = new StreamWriter(FStream1, Encoding.Default);
               foreach (string li in list)
               {
                   StrWriter.WriteLine(li);
               }
               MessageBox.Show("是否要删除您选中的记录", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
               StrWriter.Close();
               FStream.Close();
               Scan();
            }
        }//删除备忘录

你可能感兴趣的:(做备忘录(前台删除功能) 数组列表)