TXTメモ帳ファイルをデータベースとして利用し,添削保存などの操作を実現する(自用メモ)

5361 ワード

エンティティークラス:
    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 temp = new List();
            var dt = JsonHelper.JsonToObject(jsonStr.Trim(), temp);
            if (dt != null)
            {
                this.dataGridView1.DataSource = dt;               
            }
        }

1行のデータを追加するには、次の手順に従います.
//  ,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 temp = new List();
                    var dt = JsonHelper.JsonToObject(jsonStr.Trim(), temp);
                    sr.Close();
                    fs.Close();

                    if (dt != null)
                    {
                        List list = (List)dt;//object List
                        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();//    
                    }

1行のデータを削除するには、次の手順に従います.
FileStream fs = new FileStream(Application.StartupPath + "\\db.txt", FileMode.Open);
                            StreamReader sr = new StreamReader(fs, Encoding.Default);
                            var jsonStr = sr.ReadToEnd();
                            List Entity = new List();
                            var dt = JsonHelper.JsonToObject(jsonStr.Trim(), Entity);
                            sr.Close();
                            fs.Close();

                            List list = (List)dt;//object List
                            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();

セットソースのダウンロード:https://download.csdn.net/download/djk8888/10473477