C# winform 记住密码实现代码

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 
  9 namespace Remember
 10 {
 11     public partial class Form1 : Form
 12     {
 13         public Form1()
 14         {
 15             InitializeComponent();
 16         }
 17         //登录
 18         private void btn_Login_Click(object sender, EventArgs e)
 19         {
 20             //记住密码
 21             if (cb_remember.Checked == true)
 22             {
 23                 WriteIni("My Section", this.tb_UserName.Text.ToString(), this.tb_Password.Text.ToString(),
 24                     string.Format(@"{0}\xtflz.dll", Application.StartupPath));
 25                 MessageBox.Show("写入成功");
 26             }
 27             else
 28             {
 29                 WriteIni("My Section", this.tb_UserName.Text.ToString(),"",
 30                         string.Format(@"{0}\xtflz.dll", Application.StartupPath));
 31             }
 32         }
 33         #region 登录记住密码
 34         /// <summary>
 35         /// 提供INI文件的写操作(如Key和Value都为空(null), 则删除Section指定的节下所有键值(包括节名)[如Value为空(null), 则删除Section节下Key键值])       
 36         /// </summary>        
 37         /// <param name="Section">指定的节名</param>        
 38         /// <param name="Key">指定的键名</param>        
 39         /// <param name="Value">Key的值(请将相应的类型ing,long...转换为string类型)</param>        
 40         /// <param name="FilePath">INI文件全路径</param>        
 41         /// <returns></returns>        
 42         public static bool WriteIni(string Section, string Key, string Value, string FilePath)
 43         {
 44             //成功返回非零            
 45             long lRe = WritePrivateProfileString(Section, Key, Value, FilePath);
 46             return lRe == 0L ? false : true;
 47         }
 48         /// <summary>        
 49         /// 提供INI文件的读操作        
 50         /// </summary>        
 51         /// <param name="Section">指定的节名</param>        
 52         /// <param name="Key">指定的键名</param>        
 53         /// <param name="FilePath">INI文件全路径</param>        
 54         /// <returns>请将string类型转换为相应int,long的类型(返回值不应超过255字符)</returns>        
 55         public static string ReadIni(string Section, string Key, string FilePath)
 56         {
 57             int Size = 255;
 58             StringBuilder ReStr = new StringBuilder(255);
 59             GetPrivateProfileString(Section, Key, "ERROR...", ReStr, Size, FilePath);
 60             if (ReStr.ToString() == "ERROR...")
 61             {
 62                 return null;
 63             }
 64             return ReStr.ToString();
 65         }
 66         /// <summary>        
 67         /// C#申明INI文件的写操作函数WritePrivateProfileString()
 68         /// </summary>        
 69         /// <param name="Section"></param>
 70         /// <param name="Key"></param>
 71         /// <param name="Value"></param>        
 72         /// <param name="FilePath"></param>        
 73         /// <returns></returns>        
 74         //读写INI文件功能        
 75         [System.Runtime.InteropServices.DllImport("kernel32")]
 76         public static extern long WritePrivateProfileString(string Section,
 77             //指定的节名    
 78             string Key,
 79             //指定的键名           
 80             string Value,
 81             string FilePath);
 82         /// <summary>        
 83         /// C#申明INI文件的读操作函数GetPrivateProfileString        
 84         /// </summary>        
 85         /// <param name="Section"></param>        
 86         /// <param name="key"></param>        
 87         /// <param name="Def"></param>        
 88         /// <param name="RetVal"></param>        
 89         /// <param name="Size"></param>        
 90         /// <param name="FilePath"></param>        
 91         /// <returns></returns>        
 92         [System.Runtime.InteropServices.DllImport("kernel32")]
 93         public static extern int GetPrivateProfileString(string Section,
 94             //指定的节名 
 95             string key,
 96             //指定的键名 
 97             string Def,
 98             //如果未取得正确的值则返回自定义的字符串
 99             StringBuilder RetVal,
100             //保存字符串值
101             int Size,
102             //指定RetVal的长度 
103             string FilePath);
104         //ini文件路径(如果ini文件不在操作系统文件夹内,则必须指定ini文件的绝对路径)
105         #endregion 登录记住密码
106         private void tb_UserName_TextChanged(object sender, EventArgs e)
107         {
108             string s = this.tb_UserName.Text.ToString();
109             string result = ReadIni("My Section",s,string.Format(@"{0}\xtflz.dll",Application.StartupPath));
110             if (result == null || result == "")
111             {
112                 this.tb_Password.Text = "";
113                 this.cb_remember.Checked = false;
114             }
115             else
116             {
117                 this.tb_Password.Text = result;
118                 this.cb_remember.Checked = true;
119             }
120         }
121     }
122 }
 
 

 

转载于:https://www.cnblogs.com/xtflz/p/5142040.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Winform记住密码代码示例: 在窗体上添加两个文本框和一个复选框,分别命名为txtUsername、txtPassword和chkRemember,以及一个按钮命名为btnLogin。在窗体的Load事件中添加以下代码: private void Form1_Load(object sender, EventArgs e) { // 从配置文件中读取用户名和密码 if (Properties.Settings.Default.RememberMe) { txtUsername.Text = Properties.Settings.Default.Username; txtPassword.Text = Properties.Settings.Default.Password; chkRemember.Checked = true; } } 在btnLogin的Click事件中添加以下代码: private void btnLogin_Click(object sender, EventArgs e) { // 验证用户名和密码 if (txtUsername.Text == "admin" && txtPassword.Text == "password") { MessageBox.Show("登录成功!"); // 如果勾选了“记住密码”,则将用户名和密码保存到配置文件中 if (chkRemember.Checked) { Properties.Settings.Default.Username = txtUsername.Text; Properties.Settings.Default.Password = txtPassword.Text; Properties.Settings.Default.RememberMe = true; Properties.Settings.Default.Save(); } else { Properties.Settings.Default.Username = ""; Properties.Settings.Default.Password = ""; Properties.Settings.Default.RememberMe = false; Properties.Settings.Default.Save(); } } else { MessageBox.Show("用户名或密码错误!"); } } 这个代码示例使用了.NET的配置文件(App.config)来保存用户名和密码。在程序运行时,如果勾选了“记住密码”,则将用户名和密码保存到配置文件中。如果未勾选“记住密码”,则将用户名和密码清空。在下一次打开程序时,如果之前勾选了“记住密码”,则自动填充用户名和密码文本框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值