读取文本方式的简单登录

       //定义哈希表
private Hashtable ht = new Hashtable();
private void Form1_Load(object sender, EventArgs e)
{
StreamReader sr = null;
try
{
//参数一:路径,参数二:编码格式
sr = new StreamReader(@"C:\Users\Administrator\Desktop\users.txt",
Encoding.GetEncoding("gb2312"));
string str = string.Empty;
//循环读取文本,每次读取一行
while ((str = sr.ReadLine()) != null)
//将账号密码以键值对的方式添加进入哈希表
ht.Add(str.Split(' ')[0], str.Split(' ')[1]);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sr.Close();
sr.Dispose();
}
}

private void button1_Click(object sender, EventArgs e)
{
int count = 0;
//循环遍历哈希表
foreach(DictionaryEntry de in ht)
{
//如果某一键值对跟账号密码相匹配则累加1
if (de.Key.Equals(this.textBox1.Text) && de.Value.Equals(this.textBox2.Text))
count ++;
}
if (count > 0)
MessageBox.Show("登录成功!");
else
MessageBox.Show("登录失败");
}
code in my life.
原文地址:https://www.cnblogs.com/ghypnus/p/2416038.html