我目前正在开展一个学校项目,该项目将包含您经典的登录/注册表单。该项目要求我们包含一个二进制文件来“安全”存储每个用户的信息,例如用户名、密码等。
当新用户注册时,我成功地将其信息写入名为“users”的二进制文件,然后我成功地将其读回富文本框。 (这只是为了让我知道所有内容都被读回)。
当最后一个注册用户输入到表单的登录部分时,我也能够成功识别该用户,但我无法识别任何其他用户的信息。
我的注册代码(例如,当单击注册表按钮时)
private void btnRegister_Click(object sender, EventArgs e)
{
//Try - Catch Statements
//Try to set the forename
try
{
newUser.Forename = txtForename.Text;
errForename.Icon = Properties.Resources.CorrectIcon;
errForename.SetError(txtForename, "OK");
}
// catch the exception error if one is raised and place the message
//into the ErrorProvider
catch (NewException exc)
{
errForename.SetError(txtForename, exc.MessageM);
}
//Try to set the surname
try
{
newUser.Surname = txtSurname.Text;
errSurname.Icon = Properties.Resources.CorrectIcon;
errSurname.SetError(txtSurname, "OK");
}
// catch the exception error if one is raised and place the message
//into the ErrorProvider
catch (NewException exc)
{
errSurname.SetError(txtSurname, exc.MessageM);
}
//Try to set the password
try
{
newUser.Password = txtPasswordR.Text;
errPasswordR.Icon = Properties.Resources.CorrectIcon;
errPasswordR.SetError(txtPasswordR, "OK");
}
// catch the exception error if one is raised and place the message
//into the ErrorProvider
catch (NewException exc)
{
errPasswordR.SetError(txtPasswordR, exc.MessageM);
}
// These lines of code set the User class
if (newUser.Password != null )
{
newUser.Forename = txtForename.Text;
newUser.Surname = txtSurname.Text;
newUser.Username = txtUsernameR.Text;
newUser.Password = txtPasswordR.Text;
FileStream fileStream = new FileStream("../../Textfiles/users.bin", FileMode.Append);
BinaryWriter binaryWriter = new BinaryWriter(fileStream);
{
binaryWriter.Write(newUser.Forename);
binaryWriter.Write(newUser.Surname);
binaryWriter.Write(newUser.Username);
binaryWriter.Write(newUser.Password);
}
binaryWriter.Flush();
binaryWriter.Close();
}
}
我的从 Bin 文件读取的代码
public bool ReadFromBin(string a, string b)
{
bool okFlag = false;
FileStream fileStream = File.OpenRead("../../TextFiles/users.bin");
BinaryReader binaryReader = new BinaryReader(fileStream);
while (binaryReader.PeekChar() != -1)
{
newUser.Forename = binaryReader.ReadString();
newUser.Surname = binaryReader.ReadString();
newUser.Username = binaryReader.ReadString();
newUser.Password = binaryReader.ReadString();
if ((newUser.Username == a) && (newUser.Password == b))
okFlag = true;
else
okFlag = false;
}
binaryReader.Close();
return okFlag;
}
最后是非常简单的登录代码
private void btnLogIn_Click(object sender, EventArgs e)
{
if(this.ReadFromBin(txtUsername.Text, txtPassword.Text) == false)
{
MessageBox.Show("Not registered!");
}
else
{
MessageBox.Show("Registered!");
Game frm = new Game(newUser);
frm.Show();
}
}
我的理解是问题很可能出在 ReadFromBin 方法中包含的 while 循环中,我只是不确定如何修复它。谢谢!
最佳答案
我相信您的问题出在状况上。
您始终在测试用户是否是 (a,b),并且只有当最后一个用户是 (a,b) 时您的代码才能工作。
您需要更改以下内容:
public bool ReadFromBin(string a, string b)
{
bool okFlag = false;
FileStream fileStream = File.OpenRead("../../TextFiles/users.bin");
BinaryReader binaryReader = new BinaryReader(fileStream);
while (binaryReader.PeekChar() != -1)
{
newUser.Forename = binaryReader.ReadString();
newUser.Surname = binaryReader.ReadString();
newUser.Username = binaryReader.ReadString();
newUser.Password = binaryReader.ReadString();
if ((newUser.Username == a) && (newUser.Password == b))
{
okFlag = true;
break;
}
}
binaryReader.Close();
return okFlag;
}
关于使用二进制文件的 C# Winforms 登录表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35831262/