Advertisement
tremaun22

Untitled

Aug 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.Serialization;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace Calender
  11. {
  12. public class AccountDatabase
  13. {
  14. private List<String> account = new List<String>();
  15. private FileStream fs = new FileStream("c:/dev/cds.txt", FileMode.Open);
  16.  
  17. public AccountDatabase()
  18. {
  19.  
  20. }
  21.  
  22. public void AddAccount()
  23. {
  24. bool keepGoing = true;
  25.  
  26. while (keepGoing)
  27. {
  28. try
  29. {
  30. //Reads the File Database//
  31. IFormatter formatter = new BinaryFormatter();
  32. Account d = (Account)formatter.Deserialize(fs);
  33. String tre = d.ToString();
  34. account.Add(tre);
  35. }
  36. catch (SerializationException e)
  37. {
  38. keepGoing = false;
  39. }
  40. }
  41. //Request for Registry//
  42. bool isNotValid = true;
  43. Console.Write("Enter Username: ");
  44. String username = Console.ReadLine();
  45. //Checks for the Validity of the Username//
  46. while (isNotValid)
  47. {
  48. if (account.Contains(username))
  49. {
  50. Console.WriteLine("Username Already Taken");
  51. }
  52. else
  53. {
  54. isNotValid = false;
  55. }
  56. }
  57. Console.Write("Enter Password: ");
  58. String password = Console.ReadLine();
  59. Account newA = new Account(username, password);
  60. //Adds new Account to the Database
  61. MemoryStream stream = SerializeToStream(newA);
  62. byte[] Mem = stream.ToArray();
  63. fs.Seek(fs.Length, SeekOrigin.Begin);
  64. fs.Write(Mem, 0, Mem.Length);
  65.  
  66.  
  67. }
  68.  
  69.  
  70. public static MemoryStream SerializeToStream(Account o)
  71. {
  72. MemoryStream stream = new MemoryStream();
  73. IFormatter formatter = new BinaryFormatter();
  74. formatter.Serialize(stream, o);
  75. Console.WriteLine(stream);
  76. return stream;
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement