Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- List<Account> users = new List<Account>();
- // you could also (if you want to) do either of the following :
- // string path = "D:\\C#\\myAccounts.txt";
- // string path = "D:/C#/myAccounts.txt";
- string path = @"D:\C#\myAccounts.txt";
- // output all of the information already in the file to the screen so the user can
- // have all of the information about what UN/PW combos have already been entered.
- using (StreamReader sr = new StreamReader(path))
- {
- string line = sr.ReadToEnd();
- Console.WriteLine(line);
- }
- bool willContinue = true;
- do
- {
- // just put these within the scope of this do/while since you are not using them
- // anywhere outside this loop.
- string userName;
- string password;
- /**************************************************************************
- *
- * If this works correctly, it will output the line :
- *
- * Enter your username or type "exit" to quit
- *
- *
- **************************************************************************/
- Console.WriteLine(("Enter your username or type 'exit' to quit").ToString());
- // if it doesn't work, comment it out and just use this :
- Console.WriteLine("Enter your username or type 'exit' to quit");
- // gets the user input
- userName = Console.ReadLine();
- // if the user types anything other than "exit", it is a username
- if (userName != "exit")
- {
- Console.WriteLine("Enter your password:");
- password = Console.ReadLine();
- Account account = new Account(userName,password);
- users.Add(account);
- }
- // exit was entered, no more username/password combos to enter
- else
- {
- willContinue = false;
- }
- } while (willContinue);
- // open up a file stream using the StreamWriter class
- // reference : https://msdn.microsoft.com/en-us/library/36b035cb(v=vs.110).aspx
- using (StreamWriter sw = new StreamWriter(path, true))
- {
- // loop through all of the accounts entered and append them to the end of the file
- foreach (Account account in users)
- {
- // output to the console the current username and password being written to the file
- Console.WriteLine(" Username: " + account.userName + " Password: " + account.passWord);
- // write the current username and password to the file, then add an additional space between
- // each set of user name and password combinations.
- sw.WriteLine(" Username: " + account.userName/*.ToString()*/);
- sw.WriteLine(" Password: " + account.passWord/*.ToString()*/ + "\r\n");
- }
- }
- ////////////////////////////////////
- //
- // no idea what this is for??
- //
- ////////////////////////////////////
- Console.ReadLine();
- }
- }
Add Comment
Please, Sign In to add comment