博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WinForm——记住密码
阅读量:5350 次
发布时间:2019-06-15

本文共 4757 字,大约阅读时间需要 15 分钟。

 

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

方法三:注册表

转载于:https://www.cnblogs.com/songxm/p/3280787.html

你可能感兴趣的文章
AngularJs练习Demo14自定义服务
查看>>
关于空想X
查看>>
CF1067C Knights 构造
查看>>
[BZOJ2938] 病毒
查看>>
webstorm修改文件,webpack-dev-server不会自动编译刷新
查看>>
Scikit-learn 库的使用
查看>>
CSS: caption-side 属性
查看>>
python 用数组实现队列
查看>>
认证和授权(Authentication和Authorization)
查看>>
Mac上安装Tomcat
查看>>
CSS3中box-sizing的理解
查看>>
传统企业-全渠道营销解决方案-1
查看>>
Lucene全文检索
查看>>
awk工具-解析1
查看>>
推荐一款可以直接下载浏览器sources资源的Chrome插件
查看>>
CRM product UI里assignment block的显示隐藏逻辑
查看>>
AMH V4.5 – 基于AMH4.2的第三方开发版
查看>>
Web.Config文件配置之配置Session变量的生命周期
查看>>
mysql导入source注意点
查看>>
linux下编译安装nginx
查看>>