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. Servlet中文乱码处理

                                                       }                             }                   ...

  2. HDU 4169 UVALive 5741 Wealthy Family

    树形背包.DP递推的思路很简单.... 但是由于节点有15万个,先不论空间复杂度,这样开dp数组 dp[150000+10][300+10],如果初始化是memset(dp,-1,sizeof dp) ...

  3. window环境变量

    首先Window中有很多乱七八糟的路径变量之类的,归类下来有几类,主要是为了我们分清楚概念,以免搞的糊涂了. 1. Window系统的环境变量:顾名思义,就是系统级别的变量,或者利用我们编程的角度来讲 ...

  4. MySQL测试环境遇到 mmap(xxx bytes) failed; errno 12解决方法

    查看Mysql日志 InnoDB: Initializing buffer pool, size = 128.0M InnoDB: mmap( bytes) failed; errno InnoDB: ...

  5. JS脚本语言(全称java script:网页里使用的脚本语言:非常强大的语言):基础语法

    一.注释语法 1.单行注释// 2.多行注释/**/ 二.语法输出 1.alert(信息):弹出信息 2.confirm(信息):弹出一个和用户交互的对话框 3.prompt(信息):弹出一个可以让用 ...

  6. Ehcache jgroups方式同步缓存出现问题总结

    ehcache配置文件按官网配置如下: <?xml version="1.0" encoding="UTF-8"?> <ehcache> ...

  7. 理解javascript:void(0);和href="#"

    我们经常可以看见这样的代码: <a href="javascript:void(0);">链接</a> void的意思是空的,无效的意思,但是在js中是表示 ...

  8. handler的使用

    2014-04-15 10:45:06 简单学习了handler的使用. 昨天下载的问题,在手机上正常,在平板上不正常. 怀疑是网络的问题. 一直获得的流为空 2014-04-15 18:10:59 ...

  9. Python装饰器学习(九步入门)

    这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- coding:gbk -*- '''示 ...

  10. AutoLayout没有相对比例布局

    怎么实现相对比例布局 比如我一个控件相对上边距的位置在整个屏幕的比例 可以用stack view来管理相对布局