把博客园图标替换成自己的图标
把博客园图标替换成自己的图标end

用TXT记事本文件充当数据库,实现增删存等操作(自用笔记)

用TXT记事本文件充当数据库,实现增删存等操作(自用笔记)

 版权声明:相互学习,共同进步 https://blog.csdn.net/djk8888/article/details/80662660

实体类:

  1.  
    public class FtpEntity
  2.  
    {
  3.  
    //编号
  4.  
    public int ID { get; set; }
  5.  
    //文件名(不含扩展名)
  6.  
    public string FileName { get; set; }
  7.  
    //扩展名
  8.  
    public string FileType { get; set; }
  9.  
    //文件路径
  10.  
    public string FileUrl { get; set; }
  11.  
    //完整文件名
  12.  
    public string FileFullName { get; set; }
  13.  
    //上传时间
  14.  
    public DateTime? UploadTime { get; set; }
  15.  
    }

测试用例(json字符串):

  1.  
    [
  2.  
    { "ID": 1,"FileName":"文件1","FileType":".txt","FileFullName":"文件1.txt","FileUrl":"TXT文档/20180606","UploadTime":"2018-6-6 16:10:56"},
  3.  
    { "ID": 2,"FileName":"文件2","FileType":".txt","FileFullName":"文件2.txt","FileUrl":"TXT文档/20180606","UploadTime":"2018-6-6 16:11:56"}
  4.  
    ]

相关引用:

  1.  
    using Newtonsoft.Json;
  2.  
     
  3.  
    namespace winform_ftp
  4.  
    {
  5.  
    public class JsonHelper
  6.  
    {
  7.  
    // 从一个对象信息生成Json串
  8.  
    public static string ObjectToJson(object obj)
  9.  
    {
  10.  
    return JsonConvert.SerializeObject(obj);
  11.  
    }
  12.  
    // 从一个Json串生成对象信息
  13.  
    public static object JsonToObject(string jsonString, object obj)
  14.  
    {
  15.  
    return JsonConvert.DeserializeObject(jsonString, obj.GetType());
  16.  
    }
  17.  
    }
  18.  
    }

读取txt中json转化成list:

  1.  
    private void Bind()
  2.  
    {
  3.  
    FileStream fs = new FileStream(Application.StartupPath + "\\db.txt", FileMode.Open);
  4.  
    StreamReader sr = new StreamReader(fs, Encoding.Default);
  5.  
    var jsonStr = sr.ReadToEnd();//取出json字符串
  6.  
    sr.Close();
  7.  
    fs.Close();
  8.  
     
  9.  
    List<FtpEntity> temp = new List<FtpEntity>();
  10.  
    var dt = JsonHelper.JsonToObject(jsonStr.Trim(), temp);
  11.  
    if (dt != null)
  12.  
    {
  13.  
    this.dataGridView1.DataSource = dt;
  14.  
    }
  15.  
    }

添加一行数据:

  1.  
    //此处,txt文件“db.txt”充当数据库文件,用于存放、读写、删除,json数据对象集合(即json字符串)
  2.  
    FileStream fs = new FileStream(Application.StartupPath + "\\db.txt", FileMode.Open);
  3.  
    StreamReader sr = new StreamReader(fs, Encoding.Default);
  4.  
    var jsonStr = sr.ReadToEnd();
  5.  
    List<FtpEntity> temp = new List<FtpEntity>();
  6.  
    var dt = JsonHelper.JsonToObject(jsonStr.Trim(), temp);
  7.  
    sr.Close();
  8.  
    fs.Close();
  9.  
     
  10.  
    if (dt != null)
  11.  
    {
  12.  
    List<FtpEntity> list = (List<FtpEntity>)dt;//object转List<T>
  13.  
    FtpEntity entity = new FtpEntity();
  14.  
    if (list != null && list.Count > 0)
  15.  
    {
  16.  
    entity.ID = list[list.Count - 1].ID + 1;//新ID=原最大ID值+1
  17.  
    }
  18.  
    else
  19.  
    {
  20.  
    entity.ID = 1;
  21.  
    }
  22.  
    entity.FileFullName = FileFullName;
  23.  
    entity.FileName = FileName;
  24.  
    entity.FileType = FileType;
  25.  
    entity.FileUrl = FileDir;
  26.  
    entity.UploadTime = UploadTime;
  27.  
     
  28.  
    list.Add(entity);//数据集合添加一条新数据
  29.  
     
  30.  
    string json = JsonHelper.ObjectToJson(list);//list集合转json字符串
  31.  
     
  32.  
    StreamWriter sw = new StreamWriter(Application.StartupPath + "\\db.txt", false, System.Text.Encoding.UTF8);//参数2:false覆盖;true追加
  33.  
    sw.WriteLine(json);//写入文件
  34.  
    sw.Close();
  35.  
    MessageBox.Show("上传成功!");
  36.  
    Bind();//刷新列表
  37.  
    }

删除一行数据:

  1.  
    FileStream fs = new FileStream(Application.StartupPath + "\\db.txt", FileMode.Open);
  2.  
    StreamReader sr = new StreamReader(fs, Encoding.Default);
  3.  
    var jsonStr = sr.ReadToEnd();
  4.  
    List<FtpEntity> Entity = new List<FtpEntity>();
  5.  
    var dt = JsonHelper.JsonToObject(jsonStr.Trim(), Entity);
  6.  
    sr.Close();
  7.  
    fs.Close();
  8.  
     
  9.  
    List<FtpEntity> list = (List<FtpEntity>)dt;//object转List<T>
  10.  
    FtpEntity delEntity = list.Find(a => a.ID == int.Parse(ID));//根据ID值取出对象
  11.  
    list.Remove(delEntity);//从列表中删除此对象
  12.  
     
  13.  
    string json = JsonHelper.ObjectToJson(list);//将新的list转成json写入txt
  14.  
     
  15.  
    StreamWriter sw = new StreamWriter(Application.StartupPath + "\\db.txt", false, System.Text.Encoding.UTF8);//参数2:false覆盖;true追加
  16.  
    sw.WriteLine(json);//写入文件
  17.  
    sw.Close();
  18.  
     
  19.  
    MessageBox.Show("删除FTP上的文件成功!");
  20.  
    Bind();

配套源码下载:https://download.csdn.net/download/djk8888/10473477

posted on 2019-01-21 10:46  无影尊者  阅读(1995)  评论(0编辑  收藏  举报

导航

$(function(){ $(".audioplay").simplemusic({ autoplay:true, urls:["http://m1.music.126.net/gpi8Adr_-pfCuP7ZXk_F2w==/2926899953898363.mp3"] }); });