WinForm——记住密码
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——记住密码的更多相关文章
- winform 记住密码功能
//登录 private void btn_Login_Click(object sender, EventArgs e) { //记住密码 ...
- C# winform 记住密码实现代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- WinForm应用程序的开机自启、记住密码,自动登录的实现
一.思路: 1.开机自启,自然是需要用到注册表,我们需要把程序添加到电脑的注册表中去 2.记住密码,自动登录,开机自启,在页面的呈现我们都使用复选框按钮来呈现 3.数据持久化,不能是数据库,可以是sq ...
- java实现记住密码功能(利用cookie)
<br> <input type="text" id="userName" name="userName" value=& ...
- 记住密码超简单实现(C#)
实现效果如下 实现过程 [Serializable] class User { //记住密码 private string loginID; public string LoginID { get { ...
- cookie实现记住密码
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 通过sharedpreferences实现记住密码功能
通过sharedpreferences实现记住密码功能
- MiniTwitter记住密码等功能实现
一.SharedPreferences的用法:(相关实现功能的只是了解) 由于SharedPreferences是一个接口,而且在这个接口里没有提供写入数据和读取数据的能力.但它是通过其Editor接 ...
- jquery.cookie.js 操作cookie实现记住密码功能的实现代码
jquery.cookie.js操作cookie实现记住密码功能,很简单很强大,喜欢的朋友可以参考下. 复制代码代码如下: //初始化页面时验证是否记住了密码 $(document).ready( ...
随机推荐
- Xcode8 去除系统日志输出
Edit Scheme-> Run -> Arguments, 在Environment Variables里边添加 OS_ACTIVITY_MODE = disable
- jquery中,使用append增加元素时,该元素的绑定监听事件失效
举例:如果在一个<div id="resultArea"></div>中,通过append添加一个id="checkOutTip"的文本 ...
- cpptoolstip界面提示库使用
很多时候写一些界面程序,需要实时的给用户提示,每次都弹出一个messagebox挺烦的,状态栏又怕人看不到,最后找了一个相关的类,显示效果不错,分享一下,效果如下 下载库下来之后对应八个文件 将八个文 ...
- stm32 DMA数据搬运 [操作寄存器+库函数](转)
源:stm32 DMA数据搬运 [操作寄存器+库函数] DMA(Direct Memory Access)常译为“存储器直接存取”.早在Intel的8086平台上就有了DMA应用了. ...
- STM32 USB 问题汇总(转)
源:STM32 USB 问题汇总 汇总1:STM32的USB例程修改步骤,来自http://blog.csdn.net/cy757/archive/2010/01/01/5117610.aspx 以下 ...
- 统计学常用概念:T检验、F检验、卡方检验、P值、自由度
1,T检验和F检验的由来 一般而言,为了确定从样本(sample)统计结果推论至总体时所犯错的概率,我们会利用统计学家所开发的一些统计方法,进行统计检定. 通过把所得到的统计检定值,与统计学家建立了一 ...
- CentOS搭建jdk
一.检查是否安装JDK 一般安装好的CentOS会自带jdk, java -version rpm -qa | grep java 显示如下信息: java-1.4.2-gcj-compat-1.4. ...
- Python3基础 使用 in notin 查询一个字符是否指定字典的键或者值
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- 用while判读循环语句1+1/2!+1/3!+...1/20!的和阶乘的计算方法 式:n!=n*(n-1)!
package com.chongrui.test; /* *用while判读循环语句1+1/2!+1/3!+...1/20!的和 *使用BigDecimal类完成大数字与高精度运算 公式:n!=n* ...
- PHP常用函数(收集)
<?php //===============================时间日期=============================== //y返回年最后两位,Y年四位数,m月份数字 ...