先定义user类:
using System;
using System.Collections.Generic;
using System.Text;
[Serializable]
public class User
{
private string loginID = string.Empty;
public string LoginID
{
get { return loginID; }
set { loginID = value; }
}
private string pwd = string.Empty;
public string Pwd
{
get { return pwd; }
set { pwd = value; }
}
}
页面开始位置定义:
Dictionary<string, User> users = new Dictionary<string, User>();
User user = new User();
注:会员输入框用combox下拉框
在登录成功事件写:
FileStream fs = new FileStream("data.bin", FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
user.LoginID = cbologinId.Text.Trim();
if (cbRember.Checked)
{
user.Pwd = txt_userPwd.Text.Trim();
}
else
{
user.Pwd = "";
}
if (users.ContainsKey(user.LoginID))
{
users.Remove(user.LoginID);
}
users.Add(user.LoginID, user);
bf.Serialize(fs, users);
fs.Close();
页面加载的时候:
FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);
if (fs.Length > 0)
{
BinaryFormatter bf = new BinaryFormatter();
users = bf.Deserialize(fs) as Dictionary<string, User>;
foreach (User user in users.Values)
{
cbologinId.Items.Add(user.LoginID);
}
for (int i = 0; i < users.Count; i++)
{
if (cbologinId.Text.Trim() != "")
{
if (users.ContainsKey(cbologinId.Text.Trim()))
{
txt_userPwd.Text = users[cbologinId.Text].Pwd;
cbRember.Checked = true;
}
}
}
}
fs.Close();
combox用户列表选择事件:
FileStream fs = new FileStream("data.bin", FileMode.OpenOrCreate);
if (fs.Length > 0)
{
BinaryFormatter bf = new BinaryFormatter();
users = bf.Deserialize(fs) as Dictionary<string, User>;
for (int i = 0; i < users.Count; i++)
{
if (cbologinId.Text.Trim() != "")
{
if (users.ContainsKey(cbologinId.Text.Trim()) && users[cbologinId.Text].Pwd != "")
{
txt_userPwd.Text = users[cbologinId.Text].Pwd;
cbRember.Checked = true;
}
else
{
txt_userPwd.Text = "";
cbRember.Checked = false;
}
}
}
}
fs.Close();