|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPASmartClient.Helper
- {
- public class DataRecord
- {
-
- /// <summary>
- /// 读取序列化文件
- /// </summary>
- /// <param name="FilePath">需要读取序列化文件的文件名,包括后缀名</param>
- /// <returns>返回序列化读取到的信息,需要自行转换成对应的类型</returns>
- public ReadT Read<ReadT>(string FilePath)
- {
- if (File.Exists(FilePath))
- {
- FileStream fs = new FileStream(FilePath, FileMode.Open);
- BinaryFormatter bf = new BinaryFormatter();
- var res = (ReadT)bf.Deserialize(fs);
- fs.Close();
- return res;
- }
- return default;
- }
-
- /// <summary>
- /// 保存序列化文件
- /// </summary>
- /// <param name="obj">需要通过序列化保存的对象</param>
- /// <param name="FileName">设置保存的文件名,包括后缀名,后缀名可以自定义</param>
- public void Save<SaveT>(SaveT obj, string FileName)
- {
- Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\ProductionData"));
- string path = $"{AppDomain.CurrentDomain.BaseDirectory}AccessFile\\ProductionData\\{FileName}";
- if (obj != null)
- {
- FileStream fs = new FileStream(path, FileMode.Create);
- BinaryFormatter bf = new BinaryFormatter();//创建一个二进制格式化器
- bf?.Serialize(fs, obj);
- fs.Close();
- }
- }
-
- }
- }
|