Advertisement
Guest User

Untitled

a guest
Sep 15th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. private bool ReadTableFromFile()
  2.         {
  3.             System.Data.DataTable curr_table = dgv.DataSource as System.Data.DataTable;
  4.             System.IO.StreamReader strr = null;
  5.             DateTime now = DateTime.Now;
  6.             gDict.Clear();
  7.             if (!System.IO.File.Exists(filePath + @"\" + fileName)) return false;
  8.             try
  9.             {
  10.                 strr = new System.IO.StreamReader(filePath + @"\" + fileName);
  11.                 if (strr.ReadLine().Equals("__FILE_BEGIN__"))//file is correct
  12.                 {
  13.                     do
  14.                     {
  15.                         string buffer = strr.ReadLine();
  16.                         if (buffer.Equals("__EOF__"))
  17.                         {
  18.                             strr.Close();
  19.                             fileOpenWasSuccessful = true;
  20.                             dgv.Sort(dgv.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
  21.                             return true;
  22.                         }
  23.                         if (buffer.Equals("__LINE_BEGIN__"))//file is correct
  24.                         {
  25.                             System.Data.DataRow theRow = curr_table.NewRow();
  26.                             string[] preparedString;
  27.                             do
  28.                             {
  29.                                 preparedString = strr.ReadLine().Split('=');
  30.                                 if (preparedString[0].Equals("Date"))
  31.                                 {
  32.                                     DateTime dt = new DateTime();//15.03.2013 3:01:13 //dd.MM.yyyy hh:mm:ss
  33.                                     dt = DateTime.ParseExact(preparedString[1], "dd.MM.yyyy hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
  34.                                     TimeSpan timediff = (now - dt);
  35.                                     theRow[preparedString[0]] = timediff;
  36.                                     preparedString = strr.ReadLine().Split('=');
  37.                                     if (preparedString[0].Equals("IP"))
  38.                                     {
  39.                                         theRow[preparedString[0]] = preparedString[1];
  40.                                         gDict.Add(preparedString[1], dt);
  41.                                         curr_table.Rows.Add(theRow);
  42.                                         if (strr.ReadLine().Equals("__EOL__"))
  43.                                             break;
  44.                                         else throw new fileDataIncorrectException();
  45.                                     }
  46.                                     else throw new fileDataIncorrectException();
  47.                                 }
  48.                                 else throw new fileDataIncorrectException();
  49.                             } while (true);
  50.                         }
  51.                         else throw new fileDataIncorrectException();
  52.                     }
  53.                     while (strr.Peek() != -1);                
  54.                     return true;
  55.                 }
  56.                 else throw new fileDataIncorrectException();
  57.             }
  58.             catch (fileDataIncorrectException ex) { System.Windows.Forms.MessageBox.Show("Bad settings file\n" + ex.Message); return false; }
  59.             catch (Exception ex) { System.Windows.Forms.MessageBox.Show("THERE WERE UNHANDLED ERROR DURING READING FILE!\n" + ex.Message); return false; }
  60.             finally { if (strr != null) strr.Close(); }
  61.             //return true;
  62.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement