注册表操作类
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Security.AccessControl;
namespace Zone
{
/// <summary>
/// 类似qq一样保存当前登录用户的信息
/// </summary>
public class SaveUserPwd
{
private static string _CompanyName="PuTian_Ltd";
private static string _ApplicationName = "PT_SmsSend";
/// <summary>
/// 设置注册表项的公司名称
/// </summary>
public string CompanyName
{
set { _CompanyName = value; }
get { return _CompanyName; }
}
/// <summary>
/// 设置应用程序的名称
/// </summary>
public string ApplicationName
{
set { _ApplicationName = value; }
get { return _ApplicationName; }
}
//保存用户信息
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)
{
throw 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 = GetStringRegistryValue("sys_info" + (i + 1).ToString(), dedault);
}
}
catch (System.Exception ex)
{
userInfo = null;
}
return userInfo;
}
//注册表操作
public string GetStringRegistryValue(string key, string defaultValue)
{
RegistryKey rkCompany;
RegistryKey rkApplication;
rkCompany = Registry.CurrentUser.OpenSubKey("SOFTWARE", false).OpenSubKey(_CompanyName, false);
if (rkCompany != null)
{
rkApplication = rkCompany.OpenSubKey(_ApplicationName, true);
if (rkApplication != null)
{
foreach (string sKey in rkApplication.GetValueNames())
{
if (sKey == key)
{
return (string)rkApplication.GetValue(sKey);
}
}
}
}
return defaultValue;
}
public void DeleteRegistryValue(string key)
{
RegistryKey rkSoftware;
RegistryKey rkCompany;
RegistryKey rkApplication;
rkSoftware = Registry.CurrentUser.OpenSubKey("SOFTWARE", true);
if (rkSoftware == null)
{
Registry.CurrentUser.CreateSubKey("SOFTWARE");
}
rkCompany = rkSoftware.OpenSubKey(_CompanyName, true);
if (rkCompany != null)
{
rkApplication = rkCompany.OpenSubKey(_ApplicationName, true);
if (rkApplication != null)
{
rkApplication.DeleteValue(key);
}
}
//Registry.CurrentUser.DeleteSubKey(key) ;
}
/// <summary>
/// 通用使用时,注意PuTianHuaXin变量的改变
/// </summary>
/// <param name="key"></param>
/// <param name="stringValue"></param>
public void SetStringRegistryValue(string key, string stringValue)
{
RegistryKey rkSoftware;
RegistryKey rkCompany;
RegistryKey rkApplication;
rkSoftware = Registry.CurrentUser.OpenSubKey("SOFTWARE", true);
if (rkSoftware == null)
{
Registry.CurrentUser.CreateSubKey("SOFTWARE");
}
rkCompany = rkSoftware.CreateSubKey(_CompanyName);
if (rkCompany != null)
{
rkApplication = rkCompany.CreateSubKey(_ApplicationName);
if (rkApplication != null)
{
rkApplication.SetValue(key, stringValue);
}
}
}
}
}
调用的方法
void SaveUserPwds()
{
if (checkBox1.Checked)
{
saveUserPwd.SaveUserDefault("show", textBox1.Text.Trim(), DESEncrypt.Encrypt(textBox2.Text.Trim
(),"putiansms"), "用户");
}
else
{
saveUserPwd.DeleteUserDefault();
}
}
void LoadUserAndPwd()
{
string Show = saveUserPwd.GetStringRegistryValue("sys_info1", "");
if (Show == "show") checkBox1.Checked = true;
textBox1.Text = saveUserPwd.GetStringRegistryValue("sys_info2", "");
textBox2.Text = DESEncrypt.Decrypt(saveUserPwd.GetStringRegistryValue("sys_info3", ""), "putiansms");
}