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 }