c#用户登陆页面的记住密码

求详细代码
2025-05-05 10:24:42
推荐回答(1个)
回答1:

//判断是否保存
if (checkBoxSave.Checked)
{
SaveUserDefault(txtBoxLocation.Text, txtBoxUsername.Text, txtBoxPassword.Text,
cboBoxRole.Text);
}
else
{
DeleteUserDefault();
}

//保存用户信息
public void SaveUserDefault(string location, string username, string pwd, string role)
{
try
{
SetStringRegistryValue("sys_info1", location);
SetStringRegistryValue("sys_info2", username);
SetStringRegistryValue("sys_info3", pwd);
SetStringRegistryValue("sys_info4", role);
}
catch (System.Exception ex)
{
InfoHandler.ShowException(ex);
}

}
public void DeleteUserDefault()
{
try
{
DeleteRegistryValue("sys_info1");
DeleteRegistryValue("sys_info2");
DeleteRegistryValue("sys_info3");
DeleteRegistryValue("sys_info4");
}
catch (System.Exception ex)
{

}

}

/// 获取用户保存的登录信息
public string[] LoadUserDefault()
{
string[] userInfo = new string[4];
try
{
for (int i = 0; i < 4;i++ )
{
string dedault = "";
string value = RegistryAccess.GetStringRegistryValue("sys_info" + (i + 1).ToString(), dedault);
}
}
catch (System.Exception ex)
{
userInfo = null;
}
return userInfo;

}

//注册表操作
// Method for retrieving a Registry Value.
static public string GetStringRegistryValue(string key, string defaultValue)
{
RegistryKey rkCompany;
RegistryKey rkApplication;

rkCompany = Registry.CurrentUser.OpenSubKey(SOFTWARE_KEY, false).OpenSubKey(COMPANY_NAME, false);
if (rkCompany != null)
{
rkApplication = rkCompany.OpenSubKey(APPLICATION_NAME, true);
if (rkApplication != null)
{
foreach (string sKey in rkApplication.GetValueNames())
{
if (sKey == key)
{
return (string)rkApplication.GetValue(sKey);
}
}
}
}
return defaultValue;
}

// Method for storing a Registry Value.
static public void SetStringRegistryValue(string key, string stringValue)
{
RegistryKey rkSoftware;
RegistryKey rkCompany;
RegistryKey rkApplication;

rkSoftware = Registry.CurrentUser.OpenSubKey(SOFTWARE_KEY, true);
rkCompany = rkSoftware.CreateSubKey(COMPANY_NAME);
if (rkCompany != null)
{
rkApplication = rkCompany.CreateSubKey(APPLICATION_NAME);
if (rkApplication != null)
{
rkApplication.SetValue(key, stringValue);
}
}
}

static public void DeleteRegistryValue(string key)
{
RegistryKey rkSoftware;
RegistryKey rkCompany;
RegistryKey rkApplication;

rkSoftware = Registry.CurrentUser.OpenSubKey(SOFTWARE_KEY, true);
rkCompany = rkSoftware.OpenSubKey(COMPANY_NAME,true);
if (rkCompany != null)
{
rkApplication = rkCompany.OpenSubKey(APPLICATION_NAME,true);
if (rkApplication != null)
{
rkApplication.DeleteValue(key);
}
}
//Registry.CurrentUser.DeleteSubKey(key) ;
}

//可以自己加密一下