【发布时间】:2018-02-20 18:03:29
【问题描述】:
我有一些代码,用于登录我正在编写的某些软件,代码会根据包含用户名和散列密码的文件检查用户名和密码,但是,如果我输入任何其他登录名而不是存储在在文件的第一行,if 语句的两个部分都将运行,即使如果满足第一个条件,“else”就不可能运行。
我似乎找不到代码有任何问题,当然,如果登录详细信息正确,它会登录用户,即使它给出的错误是他们无法登录。
代码:
System.IO.StreamReader file = new System.IO.StreamReader("UserData.txt");
string fileLine;
string line;
while ((line = file.ReadLine()) !=null)
{
fileLine = line;
System.Diagnostics.Debug.WriteLine(fileLine);
string len = fileLine.Length.ToString();
string usr = fileLine.Substring(0, 20);
string hash = fileLine.Substring(20, 32);
if (usr.Contains(UserNameIpt.Text) && hash == password)
{
GlobalVar.UserNameEntered = UserNameIpt.Text;
showUserPanel();
break;
}
else
{
MessageBox.Show("Error, password or username incorrect. \r\n\r\nyou may not have an account set up to use this software, please contact the system administartor for assistance.", "Login Error");
}
}
【问题讨论】:
-
您是否尝试过在调试器中单步执行此代码?我很难相信代码会在
while循环的同一迭代中执行if和else。 -
问题是你会得到文件中每一行的错误——而不是到最后,所以如果你列出了 100 个用户,你会得到 100 个消息框.....
-
是的,我想这更多的是与 while 而不是 if。
-
标签: c# if-statement conditional-statements