日期:2014-05-18 浏览次数:20987 次
private const String BIN_FILE_NAME = "Profile.dat";
//窗体加载
private void FrmLogin_Load(object sender, EventArgs e)
{
//判断是否曾经保存过密码
// 首先判断,文件是否已经存在
if (File.Exists(BIN_FILE_NAME))
{
// 如果文件已经存在,那么读取,并解密
string ProfileStr = DESEncrypt.DecryptString(TestRead());
//获取信息
//帐号
string name = ProfileStr.Substring(0, ProfileStr.IndexOf("?"));
ProfileStr = ProfileStr.Substring(ProfileStr.IndexOf("?")+1);
//密码
string pwd = ProfileStr.Substring(0, ProfileStr.IndexOf("?"));
ProfileStr = ProfileStr.Substring(ProfileStr.IndexOf("?") + 1);
//记住密码
int AutoLogin = int.Parse(ProfileStr.Substring(0));
//处理
txt_AdminName.Text = name;
txt_AdminPwd.Text = pwd;
//判断是否记住登录
if (AutoLogin == 0)
{
ckb_Rememb.Checked = true;
}
else
{
//若不记住密码。则证明不自动登录
Login(false);
}
}
}
//登录按钮被点击,证明不是自动登录
private void btn_Login_Click(object sender, EventArgs e)
{
//传参,准备对保存密码的文件进行操作
Login(true);
}
private void Login(bool first)
{
//验证帐号密码是否正确
if (DBHelper.AdminLogin(txt_AdminName.Text, txt_AdminPwd.Text).Length > 0)
{
UserHelper.DefaultPath = DBHelper.AdminLogin(txt_AdminName.Text, txt_AdminPwd.Text);//无视这里,顺便获取照片的默认储存路径而已
//保存帐号密码
UserHelper.name = txt_AdminName.Text;
UserHelper.pwd = txt_AdminPwd.Text;
//记住密码
if (first)
SaveProfile();//处理帐号密码保存文件
FrmMain fm = new FrmMain();//实例化窗体
fm.Show();//窗体显示
this.Visible = false;//登录窗体隐藏
}
else
{
MessageBox.Show("用户名或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
txt_AdminPwd.Clear();
}
}
private void SaveProfile()
{
//判断是否选择记住密码(帐号?密码?0/1)
if (ckb_Rememb.Checked == true)
{
string Profile = txt_AdminName.Text + "?" + txt_AdminPwd.Text;
//判断是否选择自动登录(0不自动、1自动)
if (ckb_AutoLogin.Checked == true)
{
Profile += "?1";
}
else
{
Profile += "?0";
}
//加密
string Encrypt = DESEncrypt.EncryptString(Profile);
//写入二进制文件
TestWrite(Encrypt);
}
else
{
//取消记住密码,自动登录功能,则删除用户信息文件
if (File.Exists(BIN_FILE_NAME))
{
// 如果文件已经存在,那么删除掉.
File.Delete(BIN_FILE_NAME);
}
}
}