用TXT记事本文件充当数据库,实现增删存等操作(自用笔记)
用TXT记事本文件充当数据库,实现增删存等操作(自用笔记)
版权声明:相互学习,共同进步 https://blog.csdn.net/djk8888/article/details/80662660
实体类:
-
public class FtpEntity
-
{
-
//编号
-
public int ID { get; set; }
-
//文件名(不含扩展名)
-
public string FileName { get; set; }
-
//扩展名
-
public string FileType { get; set; }
-
//文件路径
-
public string FileUrl { get; set; }
-
//完整文件名
-
public string FileFullName { get; set; }
-
//上传时间
-
public DateTime? UploadTime { get; set; }
-
}
测试用例(json字符串):
-
[
-
{ "ID": 1,"FileName":"文件1","FileType":".txt","FileFullName":"文件1.txt","FileUrl":"TXT文档/20180606","UploadTime":"2018-6-6 16:10:56"},
-
{ "ID": 2,"FileName":"文件2","FileType":".txt","FileFullName":"文件2.txt","FileUrl":"TXT文档/20180606","UploadTime":"2018-6-6 16:11:56"}
-
]
相关引用:
-
using Newtonsoft.Json;
-
-
namespace winform_ftp
-
{
-
public class JsonHelper
-
{
-
// 从一个对象信息生成Json串
-
public static string ObjectToJson(object obj)
-
{
-
return JsonConvert.SerializeObject(obj);
-
}
-
// 从一个Json串生成对象信息
-
public static object JsonToObject(string jsonString, object obj)
-
{
-
return JsonConvert.DeserializeObject(jsonString, obj.GetType());
-
}
-
}
-
}
读取txt中json转化成list:
-
private void Bind()
-
{
-
FileStream fs = new FileStream(Application.StartupPath + "\\db.txt", FileMode.Open);
-
StreamReader sr = new StreamReader(fs, Encoding.Default);
-
var jsonStr = sr.ReadToEnd();//取出json字符串
-
sr.Close();
-
fs.Close();
-
-
List<FtpEntity> temp = new List<FtpEntity>();
-
var dt = JsonHelper.JsonToObject(jsonStr.Trim(), temp);
-
if (dt != null)
-
{
-
this.dataGridView1.DataSource = dt;
-
}
-
}
添加一行数据:
-
//此处,txt文件“db.txt”充当数据库文件,用于存放、读写、删除,json数据对象集合(即json字符串)
-
FileStream fs = new FileStream(Application.StartupPath + "\\db.txt", FileMode.Open);
-
StreamReader sr = new StreamReader(fs, Encoding.Default);
-
var jsonStr = sr.ReadToEnd();
-
List<FtpEntity> temp = new List<FtpEntity>();
-
var dt = JsonHelper.JsonToObject(jsonStr.Trim(), temp);
-
sr.Close();
-
fs.Close();
-
-
if (dt != null)
-
{
-
List<FtpEntity> list = (List<FtpEntity>)dt;//object转List<T>
-
FtpEntity entity = new FtpEntity();
-
if (list != null && list.Count > 0)
-
{
-
entity.ID = list[list.Count - 1].ID + 1;//新ID=原最大ID值+1
-
}
-
else
-
{
-
entity.ID = 1;
-
}
-
entity.FileFullName = FileFullName;
-
entity.FileName = FileName;
-
entity.FileType = FileType;
-
entity.FileUrl = FileDir;
-
entity.UploadTime = UploadTime;
-
-
list.Add(entity);//数据集合添加一条新数据
-
-
string json = JsonHelper.ObjectToJson(list);//list集合转json字符串
-
-
StreamWriter sw = new StreamWriter(Application.StartupPath + "\\db.txt", false, System.Text.Encoding.UTF8);//参数2:false覆盖;true追加
-
sw.WriteLine(json);//写入文件
-
sw.Close();
-
MessageBox.Show("上传成功!");
-
Bind();//刷新列表
-
}
删除一行数据:
-
FileStream fs = new FileStream(Application.StartupPath + "\\db.txt", FileMode.Open);
-
StreamReader sr = new StreamReader(fs, Encoding.Default);
-
var jsonStr = sr.ReadToEnd();
-
List<FtpEntity> Entity = new List<FtpEntity>();
-
var dt = JsonHelper.JsonToObject(jsonStr.Trim(), Entity);
-
sr.Close();
-
fs.Close();
-
-
List<FtpEntity> list = (List<FtpEntity>)dt;//object转List<T>
-
FtpEntity delEntity = list.Find(a => a.ID == int.Parse(ID));//根据ID值取出对象
-
list.Remove(delEntity);//从列表中删除此对象
-
-
string json = JsonHelper.ObjectToJson(list);//将新的list转成json写入txt
-
-
StreamWriter sw = new StreamWriter(Application.StartupPath + "\\db.txt", false, System.Text.Encoding.UTF8);//参数2:false覆盖;true追加
-
sw.WriteLine(json);//写入文件
-
sw.Close();
-
-
MessageBox.Show("删除FTP上的文件成功!");
-
Bind();