Advertisement
Guest User

Untitled

a guest
Jun 13th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1.         public struct CInformation {
  2.             public string Username;
  3.             public string Password;
  4.             public string Server;
  5.         }
  6.         const int EOF = -1;
  7.         const int CR = (int)'\r';
  8.         const int LF = (int)'\n';  
  9.     public void ReadConfiguration(string path) {
  10.             if (configurations != null)
  11.             {
  12.                 //Clear the memory
  13.                 configurations.Clear();
  14.                 GC.Collect();
  15.             }
  16.             configurations = new List<CInformation>();
  17.             if(File.Exists(path)){
  18.                 FileStream str = File.Open(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
  19.                 int b=0;
  20.                 string aux = "";
  21.                 while ((b = str.ReadByte()) != EOF) {
  22.                     if (b == CR) // Windows style files(CRLF)
  23.                         continue;
  24.                     if (b == LF)// IF line end
  25.                     {
  26.                         string[] _info = aux.Split(';');
  27.                         if (_info.Length > 2) // Check if line is well formed
  28.                         {
  29.                             CInformation info = new CInformation();
  30.                             info.Username = _info[0];
  31.                             info.Password = _info[1];
  32.                             info.Server = _info[2];
  33.                             configurations.Add(info);
  34.                         }
  35.                         aux = "";
  36.                     }
  37.                     else
  38.                         aux+=((char)b).ToString();
  39.                 }
  40.                 //If any line remains
  41.                 if (aux.Length > 0)
  42.                 {
  43.                     string[] last = aux.Split(';');
  44.                     if (last.Length > 2) // Check if line is well formed
  45.                     {
  46.                         CInformation info = new CInformation();
  47.                         info.Username = last[0];
  48.                         info.Password = last[1];
  49.                         info.Server = last[2];
  50.                         configurations.Add(info);
  51.                     }
  52.                 }
  53.             }
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement