Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1.  
  2. static class Ulitityclass
  3. {
  4. public static int ReadFromFile(ref playersignup[] players)
  5. {
  6. int size = 0;
  7. Stream sr;
  8. try
  9. {
  10. sr = File.OpenRead("PlayerDetails.bin");
  11. BinaryFormatter bf = new BinaryFormatter();
  12. try
  13. {
  14. while (sr.Position < sr.Length)
  15. {
  16. players[size] = (playersignup)bf.Deserialize(sr);
  17. size++;
  18. }
  19. sr.Close();
  20. }
  21. catch (SerializationException e)
  22. {
  23. sr.Close();
  24. return size;
  25. }
  26. return size;
  27. }
  28. catch (IOException e)
  29. {
  30. }
  31. return size;
  32. }
  33.  
  34. public static void DisplayUsers()
  35. {
  36. Stream sr;
  37. playersignup player;
  38. try
  39. {
  40. sr = File.OpenRead("players.txt");
  41. BinaryFormatter bf = new BinaryFormatter();
  42. try
  43. {
  44. while (sr.Position < sr.Length)
  45. {
  46. player = (playersignup)bf.Deserialize(sr);
  47. //
  48. }
  49. }
  50. catch (SerializationException e)
  51. {
  52.  
  53. }
  54. sr.Close();
  55. sr.Dispose();
  56. }
  57. catch (IOException e)
  58. {
  59. //
  60. }
  61. }
  62. public static void DisplayusersBinary()
  63. {
  64. playersignup player;
  65. Stream sr;
  66. try
  67. {
  68. sr = File.OpenRead("PlayerDetails.bin");
  69. BinaryFormatter bf = new BinaryFormatter();
  70. try
  71. {
  72. while (sr.Position < sr.Length)
  73. {
  74. player = (playersignup)bf.Deserialize(sr);
  75. }
  76. }
  77. catch (SerializationException e)
  78. {
  79.  
  80. }
  81. sr.Close();
  82. sr.Dispose();
  83. }
  84. catch (IOException e)
  85. {
  86.  
  87. }
  88.  
  89. }
  90. public static void WriteToFile(playersignup[] players, int size)
  91. {
  92. Stream sw;
  93. BinaryFormatter bf = new BinaryFormatter();
  94. try
  95. {
  96. sw = File.Open("PlayerDetails.bin", FileMode.Create);
  97. bf.Serialize(sw, players[0]);
  98. sw.Close();
  99.  
  100. //open as append and write rest of array
  101. sw = File.Open("PlayerDetails.bin", FileMode.Append);
  102. //serialialize new player
  103. for (int x = 1; x < size; x++)
  104. {
  105. bf.Serialize(sw, players[x]);
  106.  
  107. }
  108. sw.Close();
  109.  
  110.  
  111. }
  112. catch (IOException e)
  113. {
  114. //(e.Message);
  115. }
  116. }
  117.  
  118.  
  119.  
  120. public static bool UserExists(playersignup[] players, string userna,string passwo,string ema)
  121. {
  122. foreach (playersignup player in players)
  123. {
  124. //if user is registered, the method returns true
  125. if (player != null)
  126. {
  127. if ((player.Username == userna)&&(player.Password==passwo)&&(player.Email==ema))
  128. {
  129. return true;
  130. }
  131. }
  132. }
  133. return false;
  134. }
  135.  
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement