public DataHandler() { if (!File.Exists(dataPath)) { var myfile = File.Create(dataPath); myfile.Close(); //文件创建时写入默认的公司名 GongsiNameList gn = new GongsiNameList(); gn.Add(new GongsiName() { Name = "山西杏花村汾酒厂股份有限公司" }); WriteData(gn); } }
public void WriteData(GongsiNameList gongsis) { using (StreamWriter sw = new StreamWriter(dataPath)) { try { JsonSerializer serializer = new JsonSerializer(); serializer.Converters.Add(new JavaScriptDateTimeConverter()); //serializer.NullValueHandling = NullValueHandling.Ignore; //构建Json.net写入流 JsonWriter writer = new JsonTextWriter(sw); //模型序列化并写入Json.net的JsonWriter流中 serializer.Serialize(writer, gongsis); writer.Close(); sw.Close(); }catch (Exception ex) { throw ex; } } }