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. hadoop如何查看文件系统

    1.查看当前的文件系统 [root@hadoopmaster bin]# . itemsdrwxr 00 00 /user 当然也可以以浏览器中这样查看localhost:50070   这就是had ...

  2. 远离DoS攻击 Windows Server 2016发布DNS政策

             Windows Server 2016的网络功能虽然没有获得像Docker容器和Nano Server同等重要的关注,但是管理员们应该了解的是,新的域名系统(Domain Name ...

  3. MYsql数据库ERROR总结

    描述:#Warning: Using a password on the command line interface can be insecure.#ERROR 1045 (28000): Acc ...

  4. 由浅入深Mysql优化

    选Mysql优化作为我的第一篇博文,实在是因为这个东西很有意思,也是能体现后端开发人员设计细节及逻辑分析的一个知识点. 那么来吧: 作为Mysql优化,很多人大概能跟着感觉说出如下   :  (1)常 ...

  5. UVa 496 - Simply Subsets

    题目大意:给你两个集合,判断两个集合的关系(不相交.相等.真子集和其他).简单判断就可以了,不过STL的set没有交集.并集等操作有点让人觉得不方便... #include <cstdio> ...

  6. zend framework 配置连接数据库

        Zend_Db_Adapter是zend frmaeword的数据库抽象层api.基于pdo, 你可以使用Zend_Db_Adapter连接和处理多种 数据库,包括:microsoft SQL ...

  7. Java Spring MVC项目搭建(二)——项目配置

    1.站点配置文件web.xml 每一个Spring MVC 项目都必须有一个站点配置文件web.xml,他的主要功能吗....有一位大哥已经整理的很好,我借来了,大家看看: 引用博客地址: http: ...

  8. CSS3 Transitions 你可能不知道的知识点

    如何临时让transition失效 我们给一个element设置了transition效果,但某些特殊情况,我们希望让transition临时失效.我们第一反应就是先移除transition设置,等其 ...

  9. iOS 视图调试器(Debug View Hierarchy) 之 初试牛刀

    参考:http://blog.csdn.net/th_gsb/article/details/44856795 由于iOS的界面开发大多都是用代码实现的,编写的时候,那就是看不见摸不着的情况.所以,如 ...

  10. UIStackView属性解释

    Distribution 分布: Fill:填充,会根据优先级来压缩或伸长元素 Fill Equal:全都相等,并且都填充满 Fill Proportionally:按比例填充,根据元素的内容多少的比 ...