各种网站各种账号密码太多,记不清楚,原来用个Word文档把一个个账号密码给写下来,麻烦的很,今天就写了个密码管理器。密码管理器只是用来保存一些无关紧要的账号密码(其实就是当做练习C#写的一个DEMO而已,共学习交流用),并不安全,慎用啊!
相关知识点:
1. C#操作SQLite。
首先,安装SQL ADO.NET(下载地址:http://sourceforge.net/projects/sqlite-dotnet2/ 或者 http://www.sqlite.org/download.html),安装好后到安装目录的bin目录下找到System.Data.SQLite.dll。然后,在项目中添加引用即可。接下来就是对SQLite数据库的操作了。相关代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SQLite; using System.IO; using System.Windows.Forms; namespace PwdManager { class DBManager { private static string connStr = "./pwdmanager.db"; //创建数据库 public static void CreateDB() { SQLiteConnection.CreateFile("./pwdmanager.db"); } public static string getConnStr() { if (!File.Exists("./pwdmanager.db")) { CreateDB(); initDB(); } SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder(); connstr.DataSource = connStr; return connstr.ToString(); } //数据库初始化 public static bool initDB() { ExecuteSql("create table user(username varchar(20) primary key, password varchar(20));"); ExecuteSql("create table records(name varchar(20) primary key, username varchar(100), account varchar(100), userpassword varchar(100), webaddr varchar(100), picpath varchar(100));"); if (ExecuteSql("insert into user values('qian_f', 'qian_f');") != 1) { return false; } else { return true; } } //执行Sql语句 private static int ExecuteSql(string sqlStr) { using (SQLiteConnection conn = new SQLiteConnection(getConnStr())) { try { conn.Open(); SQLiteCommand comm = conn.CreateCommand(); comm.CommandText = sqlStr; comm.CommandType = System.Data.CommandType.Text; return comm.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); return -1; } } } //sql查询语句 private static SQLiteDataReader ExecQuery(string queryStr) { using (SQLiteConnection conn = new SQLiteConnection(getConnStr())) { conn.Open(); SQLiteCommand comm = conn.CreateCommand(); comm.CommandText = queryStr; comm.CommandType = System.Data.CommandType.Text; return comm.ExecuteReader(); } } //插入记录 public static bool InsetRecord(string name, string username, string account, string password, string webaddr) { string str = "insert into records values('" + name + "', '" + username + "', '" + account + "', '" + password + "', '" + webaddr + "', '');"; if (ExecuteSql(str) != 1) { MessageBox.Show("添加记录失败!"); return false; } else return true; } //删除记录 public static bool DelRecord(string name) { string str = "delete from records where name = '" + name + "';"; if (ExecuteSql(str) != 1) { MessageBox.Show("删除记录失败!"); return true; } else { return false; } } //修改记录 public static bool UpdateRecord(string name, string username, string account, string password, string webaddr) { string str = "update records set username='" + username + "', account='" + account + "', userpassword='" + password + "', webaddr='" + webaddr + "' where name='" + name + "';"; if (ExecuteSql(str) != 1) { MessageBox.Show("修改记录失败!"); return false; } else return true; } //校验登录用户和密码 public static bool CheckLogin(string name, string password) { using (SQLiteConnection conn = new SQLiteConnection(getConnStr())) { conn.Open(); string query = "select password from user where username='" + name + "';"; SQLiteCommand comm = conn.CreateCommand(); comm.CommandText = query; comm.CommandType = System.Data.CommandType.Text; SQLiteDataReader reader = comm.ExecuteReader(); if (reader.HasRows == false) { return false; } reader.Read(); if (reader[0].ToString().Equals(password)) { return true; } else { return false; } } } } }2. C#中的剪贴板操作
//... using System.Data.SQLite; namespace PwdManager { public partial class Form1 : Form { //... private void Btn_CopyAccount_Click(object sender, EventArgs e) { if (dataGridView1.RowCount == 0) { MessageBox.Show("当前没有记录!"); return; } string account = dataGridView1.CurrentRow.Cells[2].Value.ToString(); Clipboard.SetDataObject(account, true); SetMessage("账号已经复制到剪贴板中!"); } //... } }
3. C#中使用默认浏览器打开网址
System.Diagnostics.Process.Start("www.csdn.net");4. 两个Form之间信息的传输
//... namespace PwdManager { public partial class Form1 : Form { //... //设置label字符串 public void SetMessage(string msg) { this.label_msg.Text = msg; } //添加记录 private void Btn_AddRecord_Click(object sender, EventArgs e) { InfoForm form2 = new InfoForm(true); form2.Owner = this; form2.ShowDialog(); this.LoadData(); } //... } }
//... namespace PwdManager { public partial class InfoForm : Form { //... private void btn_confirm_Click(object sender, EventArgs e) { //... if (this.isAdd == true) { DBManager.InsetRecord(name, username, account, password, webaddr); Form1 form = (Form1)this.Owner; form.SetMessage("记录添加成功!"); } //.... } //... } }
项目下载地址:http://yun.baidu.com/share/link?shareid=4261103166&uk=3508115909