`
ch_kexin
  • 浏览: 875654 次
  • 性别: Icon_minigender_2
  • 来自: 青岛
社区版块
存档分类
最新评论

如何记住密码?

    博客分类:
  • .NET
阅读更多
封装一个类,里面两个方法,一个是写,一个是读,直接调用即可
public class LogUserName
{
	public LogUserName()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    //用户名写入注册表
    public static void writeLogUserName(String userName)
    {
        //读取本机 windows基项 HKEY_LOCAL_MACHINE
        RegistryKey keyMachine = Registry.LocalMachine;
        //要打开的子项名称或路经Software
        RegistryKey keySoftware = keyMachine.OpenSubKey("Software", true);
        if (keySoftware != null)
        {
            //创建新子项或打开现有子项进行写入访问
            RegistryKey keyMy = keySoftware.CreateSubKey("MySoftware");
            if (keyMy != null)
            {
                //设置K ,V
                keyMy.SetValue("userName", userName);
            }
        }
    }
    //读取注册表中的用户名
    public static String readLogUserName()
    {
        //读取本机 windows基项 HKEY_LOCAL_MACHINE
        RegistryKey regMachine = Registry.LocalMachine;
        //以只读方式检索子项 Software
        RegistryKey regSoftware = regMachine.OpenSubKey("Software");
        if (regSoftware != null)
        {
            //以只读方式检索子项 MySoftware
            RegistryKey regMy = regSoftware.OpenSubKey("MySoftware");
            if (regMy != null)
            {
                //检索与指定名相关的值
                String userName = regMy.GetValue("userName").ToString();
                if (!String.IsNullOrEmpty(userName)) 
                {
                    return userName;
                }
               return "";
            }
        }
        return "";
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics