using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace LoginAdmin
{
    class RWini
    {
        private string Path;

[DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,string key,string val,string path);

[DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,string key, string def, StringBuilder retVal, int size, string Path);

/// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="INIPath"></param>
        public RWini(string INIPath)
        {
            Path=INIPath;
        }

/// <summary>
        /// 写入INI文件
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        public void WriteInivalue(string Section, string Key, string value)
        {
            File.SetAttributes(this.Path, FileAttributes.Normal);
            WritePrivateProfileString(Section, Key, value, this.Path);
            File.SetAttributes(this.Path, FileAttributes.ReadOnly);
        }

/// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public string ReadInivalue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(1024);
            int i = GetPrivateProfileString(Section, Key, "读取错误", temp, 1024, this.Path);
            return temp.ToString();
        }
    }
}


using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

namespace LoginAdmin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

private void Form1_Load(object sender, EventArgs e)
        {
            ReadINI();
            Roles();
            MessageBox.Show(textBox1.Text);
        }

private void Roles()
        {
            //文件操作读取-------StreamReader
            string filepath = "..\\..\\Roles.txt";
            StreamReader sr = new StreamReader(filepath, Encoding.Unicode);
            //string content = sr.ReadToEnd();//返回string
            string content = sr.ReadLine();
            sr.Close();

string[] arry = content.Split('\t');
            for (int i = 0; i < arry.Length; i++)
            {
                comboBox1.Items.Add(arry[i]);
            }
        }

/// <summary>
        /// 写入INI
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                string name = textBox1.Text;
                string password = textBox2.Text;
                string path = @"..\\..\\set.ini";
                RWini ini = new RWini(path);
                ini.WriteInivalue("mysystem", "username", name);
            }
        }

/// <summary>
        /// 读取ini
        /// </summary>
        private void ReadINI()
        {
            string path = Path.GetFullPath(@"..\\..\\set.ini");
            RWini ini = new RWini(path);
            textBox1.Text = ini.ReadInivalue("mysystem", "username");
        }

}

}


方法二:注册表

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

namespace LoginAdmin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

private void Form1_Load(object sender, EventArgs e)
        {
            Roles();
            HasRemember();
        }

private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                string name = textBox1.Text;
                string password = textBox2.Text;
                Remember(true, name, password);
            }
        }

/// <summary>
        /// 根据传入值记住密码
        /// </summary>
        /// <param name="remember">是否记录</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        private void Remember(bool remember,string userName,string password)
        {
            try{
                RegistryKey key=Registry.LocalMachine.OpenSubKey("SOFTWARE",true);
                key=key.CreateSubKey("CheckManage",RegistryKeyPermissionCheck.Default);
                if(remember)
                {
                    key.SetValue("username",userName);
                    key.SetValue("password",password);
                }
                else
                {
                    if(key.GetValue("username")!=null) key.DeleteValue("username");
                    if(key.GetValue("password")!=null) key.DeleteValue("password");
                }
            }
            catch
            {
            }
        }

/// <summary>
        /// 判断是否记住密码
        /// </summary>
        /// <returns></returns>
        private bool HasRemember()
        {
            try
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\CheckManage", true);
                object objU = key.GetValue("username");
                if (objU != null)
                {
                    this.textBox1.Text = objU.ToString();
                }
                object objP = key.GetValue("password");
                if (objP != null) this.textBox2.Text = objP.ToString();
                return objU != null && objP != null;
            }
            catch
            {
                return false;
            }
        }

private void Roles()
        {
            //文件操作读取-------StreamReader
            string filepath = "..\\..\\Roles.txt";
            StreamReader sr = new StreamReader(filepath, Encoding.Unicode);
            //string content = sr.ReadToEnd();//返回string
            string content = sr.ReadLine();
            sr.Close();

string[] arry = content.Split('\t');
            for (int i = 0; i < arry.Length; i++)
            {
                comboBox1.Items.Add(arry[i]);
            }
        }

}

}


总结:

方法一:写入数据库

方法二:写入文件——txt、xml、ini

方法三:注册表

WinForm——记住密码的更多相关文章

  1. winform 记住密码功能

      //登录        private void btn_Login_Click(object sender, EventArgs e)        {            //记住密码    ...

  2. C# winform 记住密码实现代码

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. WinForm应用程序的开机自启、记住密码,自动登录的实现

    一.思路: 1.开机自启,自然是需要用到注册表,我们需要把程序添加到电脑的注册表中去 2.记住密码,自动登录,开机自启,在页面的呈现我们都使用复选框按钮来呈现 3.数据持久化,不能是数据库,可以是sq ...

  4. java实现记住密码功能(利用cookie)

    <br> <input type="text" id="userName" name="userName" value=& ...

  5. 记住密码超简单实现(C#)

    实现效果如下 实现过程 [Serializable] class User { //记住密码 private string loginID; public string LoginID { get { ...

  6. cookie实现记住密码

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  7. 通过sharedpreferences实现记住密码功能

    通过sharedpreferences实现记住密码功能

  8. MiniTwitter记住密码等功能实现

    一.SharedPreferences的用法:(相关实现功能的只是了解) 由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力.但它是通过其Editor接 ...

  9. jquery.cookie.js 操作cookie实现记住密码功能的实现代码

    jquery.cookie.js操作cookie实现记住密码功能,很简单很强大,喜欢的朋友可以参考下.   复制代码代码如下: //初始化页面时验证是否记住了密码 $(document).ready( ...

随机推荐

  1. CodeForces 450B Jzzhu and Sequences

    矩阵快速幂. 首先得到公式 然后构造矩阵,用矩阵加速 取模函数需要自己写一下,是数论中的取模. #include<cstdio> #include<cstring> #incl ...

  2. CodeForces 626D Jerry's Protest

    计算前两盘A赢,最后一盘B赢的情况下,B获得的球的值总和大于A获得的球总和值的概率. 存储每一对球的差值有几个,然后处理一下前缀和,暴力枚举就好了...... #include<cstdio&g ...

  3. PHP的Cookie、Session和跟Laravel相关的几点了解

    这两天通过对Cookie和Session的查找和了解,网上关于它们两个的基础知识点都是差不多的,也收藏了几篇不错的博客,同时自己做了些实验后,有了以下几点了解: 1.setcookie 这里有三个地方 ...

  4. MySQL show master / slave status 命令参数

    一.show master status 二.show slave status Slave_IO_State SHOW PROCESSLIST输出的State字段的拷贝.SHOW PROCESSLI ...

  5. Linux ALSA声卡驱动之二:声卡的创建

    1. struct snd_card 1.1. snd_card是什么 snd_card可以说是整个ALSA音频驱动最顶层的一个结构,整个声卡的软件逻辑结构开始于该结构,几乎所有与声音相关的逻辑设备都 ...

  6. HTML 学习笔记 JQuery(表单,表格 操作)

    表单应用 一个表单有3个基本组成部分 (1)表单标签:包含处理表单数据所用的服务器端程序URL 以及数据提交到服务器的方法 (2)表单域:包含文本框 密码框 隐藏框 多行文本框 复选框 单选框 下拉选 ...

  7. Mac iTerm2使用rz、sz从远程上传下载文件

    使用 brew install lrzsz .如果安装遇到错误的话,使用以下方法: 在mac终端下运行: brew install lrzsz (安装教程:http://brew.sh/index_z ...

  8. 一起学JUCE之HashMap

    基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同.) ...

  9. VS2010下创建的VB.NET项目打包发布安装包的流程

    VS2010下创建的VB.NET项目打包发布安装包的流程 参考:http://blog.csdn.net/liuyanlinglanq/article/details/8609675  关于relea ...

  10. Java 之 反射

    1.反射 a.意义:允许运行中的Java程序对自身进行检查,或者说"自审",并能直接操作程序的内部属性 b.概括:运行时探究和使用编译时未知的类 c.反射的核心原理: ①JVM在加 ...