Guest User

Untitled

a guest
Oct 30th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.      
  6.         List<Account> users = new List<Account>();
  7.         // you could also (if you want to) do either of the following :
  8.         // string path = "D:\\C#\\myAccounts.txt";
  9.         // string path = "D:/C#/myAccounts.txt";
  10.         string path = @"D:\C#\myAccounts.txt";
  11.  
  12.         // output all of the information already in the file to the screen so the user can
  13.         // have all of the information about what UN/PW combos have already been entered.
  14.         using (StreamReader sr = new StreamReader(path))
  15.         {
  16.             string line = sr.ReadToEnd();
  17.             Console.WriteLine(line);
  18.         }
  19.  
  20.         bool willContinue = true;
  21.  
  22.         do
  23.         {
  24.             // just put these within the scope of this do/while since you are not using them
  25.             // anywhere outside this loop.
  26.             string userName;
  27.             string password;
  28.  
  29.             /**************************************************************************
  30.             *
  31.             *   If this works correctly, it will output the line :
  32.             *
  33.             *   Enter your username or type "exit" to quit
  34.             *
  35.             *
  36.             **************************************************************************/
  37.             Console.WriteLine(("Enter your username or type 'exit' to quit").ToString());
  38.             // if it doesn't work, comment it out and just use this :
  39.             Console.WriteLine("Enter your username or type 'exit' to quit");
  40.  
  41.             // gets the user input
  42.             userName = Console.ReadLine();
  43.  
  44.             // if the user types anything other than "exit", it is a username
  45.             if (userName != "exit")
  46.             {
  47.                 Console.WriteLine("Enter your password:");
  48.                 password = Console.ReadLine();          
  49.  
  50.                 Account account = new Account(userName,password);
  51.                 users.Add(account);
  52.             }
  53.             // exit was entered, no more username/password combos to enter
  54.             else
  55.             {
  56.                 willContinue = false;
  57.             }
  58.         } while (willContinue);
  59.  
  60.         // open up a file stream using the StreamWriter class
  61.         // reference : https://msdn.microsoft.com/en-us/library/36b035cb(v=vs.110).aspx
  62.         using (StreamWriter sw = new StreamWriter(path, true))
  63.         {
  64.             // loop through all of the accounts entered and append them to the end of the file
  65.             foreach (Account account in users)
  66.             {
  67.                 // output to the console the current username and password being written to the file
  68.                 Console.WriteLine(" Username: " + account.userName + " Password: " + account.passWord);
  69.  
  70.                 // write the current username and password to the file, then add an additional space between
  71.                 // each set of user name and password combinations.
  72.                 sw.WriteLine(" Username: " + account.userName/*.ToString()*/);
  73.                 sw.WriteLine(" Password: " + account.passWord/*.ToString()*/ + "\r\n");
  74.             }
  75.         }
  76.  
  77.         ////////////////////////////////////
  78.         //
  79.         // no idea what this is for??
  80.         //
  81.         ////////////////////////////////////
  82.         Console.ReadLine();
  83.     }
  84. }
Add Comment
Please, Sign In to add comment