终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

53 rindas
1.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BPASmartClient.Helper
  10. {
  11. public class DataRecord
  12. {
  13. /// <summary>
  14. /// 读取序列化文件
  15. /// </summary>
  16. /// <param name="FilePath">需要读取序列化文件的文件名,包括后缀名</param>
  17. /// <returns>返回序列化读取到的信息,需要自行转换成对应的类型</returns>
  18. public ReadT Read<ReadT>(string FilePath)
  19. {
  20. if (File.Exists(FilePath))
  21. {
  22. FileStream fs = new FileStream(FilePath, FileMode.Open);
  23. BinaryFormatter bf = new BinaryFormatter();
  24. var res = (ReadT)bf.Deserialize(fs);
  25. fs.Close();
  26. return res;
  27. }
  28. return default;
  29. }
  30. /// <summary>
  31. /// 保存序列化文件
  32. /// </summary>
  33. /// <param name="obj">需要通过序列化保存的对象</param>
  34. /// <param name="FileName">设置保存的文件名,包括后缀名,后缀名可以自定义</param>
  35. public void Save<SaveT>(SaveT obj, string FileName)
  36. {
  37. Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\ProductionData"));
  38. string path = $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\ProductionData\\{FileName}";
  39. if (obj != null)
  40. {
  41. FileStream fs = new FileStream(path, FileMode.Create);
  42. BinaryFormatter bf = new BinaryFormatter();//创建一个二进制格式化器
  43. bf?.Serialize(fs, obj);
  44. fs.Close();
  45. }
  46. }
  47. }
  48. }