0

I have a file-message.txt that contains raw data, my application reads the file, parses it and displays the data accordingly in the listview. The raw data contains a word called REC UNREAD meaning the record is unread. So for the first time when message is read it is UNREAD and I display such messages in bold. After I read it(Using doubleclick event) the word REC UNREAD should be changed to REC READ. This is what I have I tried, not working though

    private void lvwMessages_MouseDoubleClick_1(object sender, MouseEventArgs e)
    {
        try
        {
            ListViewItem item = lvwMessages.SelectedItems[0];
            if(item.Font.Bold)

                {
                    lvwMessages.SelectedItems[0].Font = new Font(lvwMessages.Font, FontStyle.Regular);
                    string tfile = File.ReadAllText("C:\\message.txt");
                    string m1 = lvwMessages.SelectedItems[0].SubItems[1].Text;
                    string m2 = lvwMessages.SelectedItems[0].SubItems[2].Text;
                    //No idea how to go forward from here

This is a sample line in my text file:

+CMGL: 2,"REC UNREAD","+919030665834","","2012/08/10 17:04:15+22"
sample message

In simple words I should be able to search for the line containing m1 and m2(as in the code) and replace the REC UNREAD with REC READ.

4

2 回答 2

1

This should solve your problem--

            ListViewItem item = lvwMessages.SelectedItems[0];
            if(item.Font.Bold)
                {
                    lvwMessages.SelectedItems[0].Font = new Font(lvwMessages.Font, FontStyle.Regular);
                    string tfile = File.ReadAllText("C:\\message.txt");
                    string m1 = lvwMessages.SelectedItems[0].SubItems[1].Text;
                    string m2 = lvwMessages.SelectedItems[0].SubItems[2].Text;
                    string line = string.Empty;
                    string nfile= "";
                    using (StreamReader sr = new StreamReader("C:\\message.txt"))
                    {
                      while ((line = sr.ReadLine()) != null)
                      {
                          if (line.Contains(m2)) 
                          {
                              string pline = line;
                             string result = line.Replace("REC UNREAD", "REC READ");
                             nfile= tfile.Replace(pline, result);
                          }
                       }
                   sr.Close();
                   }
                StreamWriter sw = new StreamWriter("C:\\message.txt");
                {
                    sw.Write(nfile);
                }
                sw.Close();                           
            }
于 2012-08-11T05:19:51.190 回答
0

you can try with this code based on IndexOf and Replace

string line = string.Empty;

using (StreamReader sr = new StreamReader("C:\\message.txt"))
{
  while ((line = sr.ReadLine()) != null)
  {
      if (line.IndexOf(m1) > 0 &&
      line.IndexOf(m2)  )
      {
         var result = line.Replace(m2, "READ");
      }
   }
}
于 2012-08-10T12:32:17.297 回答