讀取檔案后,出現登錄時賬戶與密碼只要是在listbox中的都能登陸成功,ex:(Tester1,2 則提示登陸成功)求大神幫忙解決,以下是程式以及txt檔案內容:

uj5u.com熱心網友回復:
回圈不對吧,一對一應該uj5u.com熱心網友回復:
用這個吧,稍微好點:用鍵值對,可以保障用戶名和密碼是一一對應的,用戶名是不應該區分大小寫的
public partial class Form1 : Form
{
private Dictionary<string, string> userPwds = new Dictionary<string, string>();
public Form1()
{
InitializeComponent();
this.LoadUsers();
}
private void LoadUsers()
{
this.userPwds = new Dictionary<string, string>();
string[] lines = File.ReadAllLines("d:\\test.txt");
foreach (string line in lines)
{
string[] userPwd = line.Split(new char[] { ',' });
if (userPwd.Length == 2)
{
if (!string.IsNullOrWhiteSpace(userPwd[0]))
{
this.userPwds.Add(userPwd[0], userPwd[1]);
}
}
}
}
private void buttonLogin_Click(object sender, EventArgs e)
{
string user = this.textBoxUser.Text;
string pwd = this.textBoxPwd.Text;
if (string.IsNullOrWhiteSpace(user))
{
MessageBox.Show("請輸入用戶名");
return;
}
int flag = 0;
foreach (var up in userPwds)
{
if (string.Compare(user, up.Key) == 0)
{
flag = 1; //用戶名對
if (string.Compare(pwd, up.Value) == 0)
{
flag = 2; //密碼對
}
break;
}
}
if (flag == 0)
{
MessageBox.Show("用戶名不存在");
return;
}
if (flag == 1)
{
MessageBox.Show("密碼錯誤");
return;
}
if (flag == 2)
{
MessageBox.Show("登錄成功");
return;
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/82427.html
標籤:C#
上一篇:使用c#呼叫c++dll,有一個讀值并傳出的函式,我想多次在c#中多次獲得更新值,但重新呼叫函式沒有數值沒有更新
