我目前正在开展一个具有您经典登录/注册表格的学校项目。该项目要求我们包含一个二进制文件,以“安全地”存储每个用户的信息,例如用户名,密码等。
我在注册时成功将新用户信息写入名为“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循环中,我只是不确定如何修复它。谢谢!
答案 0 :(得分:1)
我相信你的问题是在这种情况下。
如果用户是(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;
}
答案 1 :(得分:1)
这是问题所在:
if ((newUser.Username == a) && (newUser.Password == b))
okFlag = true;
else
okFlag = false;
该代码是为文件中的每个条目运行的。因此,如果okFlag
从第一个条目设置为true
,但还有其他条目,那么它就无关紧要了 - 因为它将被设置为false
其他条目。
对此最简单的解决方法是对using
和FileStream
使用BinaryReader
语句,这样您就不需要Close
调用,然后只需完全删除okFlag
,如果您遇到匹配的条目,则返回true
:
if (newUser.Username == a && newUser.Password == b)
{
return true;
}
然后在循环之后放置return false
,因此如果没有条目匹配,则只返回false。
当然,你真的不应该把密码放在一个文件中,但这是一个不同的问题......
请注意,using
语句的原因不仅仅是为了解决此问题 - 即使抛出异常,您也希望关闭流和阅读器。您应该总是对这样的场景使用using
语句,您可以在其中打开资源,使用它,然后关闭它 - 您希望无论如何都要进行关闭操作在您使用资源时是否抛出异常。