用户登录 记住账号密码,用二进制方式存储临时数据;其实可以记住任何对象。
1、首先,需要一个可访问的存储位置 -- 本地临时目录:
// 这两句找到当前用户的临时文件目录
string _temp = System.Environment.GetEnvironmentVariable("TEMP");
DirectoryInfo info = new DirectoryInfo(_temp);
info.FullName // 这是临时目录
方法:
1 /// <summary>
2 /// 临时文件目录
3 /// </summary>
4 /// <param name="tempPath">临时文件位置</param>
5 /// <param name="filename">临时文件名</param>
6 /// <returns>文件路径</returns>
7 private string TempPath(string tempPath,string filename)
8 {
9 // 这两句找到当前用户的临时文件目录
10 string _temp = System.Environment.GetEnvironmentVariable("TEMP");
11 DirectoryInfo info = new DirectoryInfo(_temp);
12
13 string db_path = string.Format("{0}{1}", info.FullName, tempPath);
14 // 如果文件不存在则创建
15 if (!Directory.Exists(db_path))
16 {
17 Directory.CreateDirectory(db_path);
18 }
19 string db_file = string.Format("{0}\\{1}", db_path, filename);
20 return db_file;
21 }
2、然后,写入文件
1 /// <summary>
2 /// 写入文件
3 /// </summary>
4 /// <typeparam name="T">泛型对象</typeparam>
5 /// <param name="key">存储Key</param>
6 /// <param name="db_file_path">存储文件路径</param>
7 /// <param name="content">存储内容</param>
8 /// <param name="isremember">是否记录</param>
9 private void WritBin<T>(string key, string db_file_path, T content, bool isremember)
10 {
11 Dictionary<string, T> cs = new Dictionary<string, T>();
12
13 FileStream fs = new FileStream(db_file_path, FileMode.OpenOrCreate);
14 try
15 {
16 BinaryFormatter bf = new BinaryFormatter();
17
18 //选在集合中是否存在用户名
19 if (cs.ContainsKey(key))
20 {
21 //如果有清掉
22 cs.Remove(key);
23 }
24 //添加用户信息到集合
25 if (isremember)
26 {
27 cs.Add(key, content);
28 }
29
30 //写入文件
31 bf.Serialize(fs, cs);
32 }
33 finally
34 {
35 //关闭
36 fs.Close();
37 fs.Dispose();
38 }
39 }
3、接着读取文件内容:
1 /// <summary>
2 /// 读取Bin
3 /// </summary>
4 /// <typeparam name="T"></typeparam>
5 /// <param name="path"></param>
6 /// <returns></returns>
7 private List<T> ReadBin<T>(string path)
8 {
9 //读取文件流对象
10 List<T> list = new List<T>();
11 Dictionary<string, T> cs = new Dictionary<string, T>();
12
13 //读取文件流对象
14 FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
15 try
16 {
17 if (fs.Length > 0)
18 {
19 BinaryFormatter bf = new BinaryFormatter();
20 //读出存在Data.bin 里的用户信息
21 cs = bf.Deserialize(fs) as Dictionary<string, T>;
22
23 list = cs.Values.ToList();
24
25 }
26 return list;
27 }
28 finally
29 {
30 fs.Close();
31 fs.Dispose();
32 }
33 }
4、以上都是私有方法,需要两个公共方法提供调用
1 /// <summary>
2 /// 读取bin内容
3 /// </summary>
4 /// <typeparam name="T"></typeparam>
5 /// <param name="path"></param>
6 /// <returns></returns>
7 public List<T> GetListBinValues<T>(string path,string filename)
8 {
9 try
10 {
11 var fp = TempPath(path, filename);
12 return ReadBin<T>(fp);
13 }
14 catch (Exception ex)
15 {
16 throw new Exception(ex.Message);
17 }
18 }
19
20 /// <summary>
21 /// 写入值
22 /// </summary>
23 /// <typeparam name="T">泛型对象</typeparam>
24 /// <param name="path">位置路径 (本来临时目录下的文件夹相对路径)</param>
25 /// <param name="filename">文件名</param>
26 /// <param name="isremember">是否存储</param>
27 /// <param name="key">存储key</param>
28 /// <param name="content">存储内容</param>
29 public void AddListBinValues<T>(string path,string filename,bool isremember,string key, T content)
30 {
31 try
32 {
33 var fp = TempPath(path, filename);
34
35 WritBin<T>(key,fp, content, isremember);
36 }
37 catch (Exception ex)
38 {
39 throw new Exception(ex.Message);
40 }
41
42 }
5、最后 封装到类里
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
namespace TempService
{
public class DBbin
{
// 添加以上方法
}
}
调用实例
写入:
DBbin _DBbin = new DBbin();
remberUser vm = new remberUser();
vm.username = _loginName;
vm.password = _pwd;
_DBbin.AddListBinValues(remberpath, remberfile, _isRe, _loginName, vm);
读取值:
DBbin _DBbin = new DBbin();
var users = _DBbin.GetListBinValues<remberUser>(remberpath, remberfile);
***注意事项***
[Serializable]要注明需要存储的对象是可序列化的,不然会报错
/// <summary>
/// 用户名密码记录
/// </summary>
[Serializable]
public class remberUser
{
public string username { get; set; }
public string password { get; set; }
}