读取
//读取 public static List<ConfigurationInfo> Read() { string context = null; string filePath = Environment.CurrentDirectory + "/Configuration.txt";//获取文件根目录 if (File.Exists(filePath)) { FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); //指定的路径 ,读取的模式,初始实例化 using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312"))) { context = sr.ReadToEnd(); } fs.Dispose(); fs.Close(); } else { Write(GetConfigurationList()); //写入 return GetConfigurationList(); //返回 } var _instance = JsonConvert.DeserializeObject<List<ConfigurationInfo>>(context); return _instance; }
写入
// 写入 public static bool Write(List<ConfigurationInfo> Model) { string filePath = Environment.CurrentDirectory + "/Configuration.txt"; //获取当前目录下的路径 string json = JsonConvert.SerializeObject(Model); //转化为JOSN File.WriteAllText(filePath, json, Encoding.GetEncoding("gb2312"));//写入 return true; }