I am trying to make a basic login and sign up c# console application, however, I need to loop through my file to see if the username and password the user inputted has a match when logging in. If the user types in a username and password, I want my code to go through my file to check if it is an existing username and password
Here is my code:
[Serializable]
public class Users
{
public string UserName;
public string Password;
public Users(string userName, string password)
{
UserName = userName;
Password = password;
}
}
public class SaveToFile
{
public static void SerializeSignUpDetails(string userName, string password)
{
Users obj = new Users(userName, password);
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("SignUp.txt", FileMode.Append, FileAccess.Write);
formatter.Serialize(stream, obj);
stream.Close();
}
public static Users DeserializeSignUpDetails()
{
Stream stream = new FileStream("SignUp.txt", FileMode.Open, FileAccess.Read);
IFormatter formatter = new BinaryFormatter();
Users objnew = (Users)formatter.Deserialize(stream);
stream.Close();
return objnew;
}
}
public static void Main(string[] args)
{
Console.WriteLine("To Login Type 1, To Create a new account Type 2");
int LogInOrSignUp;
do
{
int.TryParse(Console.ReadLine(), out LogInOrSignUp);
} while (LogInOrSignUp != 1 && LogInOrSignUp != 2);
string userName = "";
string password = "";
bool successfull = false;
Users userDetails = SaveToFile.DeserializeSignUpDetails();
while (!successfull)
{
if (LogInOrSignUp == 1)
{
Console.WriteLine("Write your username:");
userName = Console.ReadLine();
Console.WriteLine("Enter your password:");
password = Console.ReadLine();
if (userName == userDetails.UserName && password == userDetails.Password)
{
Console.WriteLine("You have logged in successfully!");
successfull = true;
break;
}
if (!successfull)
{
Console.WriteLine("Your username or password is incorect, try again!");
}
}
else if (LogInOrSignUp == 2)
{
Console.WriteLine("Enter a username:");
userName = Console.ReadLine();
Console.WriteLine("Enter a password:");
password = Console.ReadLine();
successfull = true;
SaveToFile.SerializeSignUpDetails(userName, password);
}
}
}
I want to use foreach to loop through my file, but I am not sure how.
Any help appreciated!
To keep a login record for multiple entries using serialization, you need to serialize a list of objects. In your case, you could create a couple of serializable classes,
Userclass that encapsulates the data of a single entry, andUsersclass that contains aList<User>objects plus the data manipulation methods.✔ Note: Name as you prefer.
◉ Namespaces to import
◉ The User class
◉ The Users class
In your implementation, to create, load, and save your data:
Use the
ContainsUserNamemethod to find out whether a given username is already exist so you can avoid the duplicates. TheAddmethod will do the same plus it will add the valid new entries into the list. TheGetmethod searches the list for the given username and password and returns aUserobject if any otherwisenull, and theContainsAccountmethod will do the same if you don't need to return aUserobject.Applying that in your
main:✔ Note: Better to use the switch statement to select
LogInOrSignUpinstead of theifstatements