public List <MessageHandler> ReadMessages() { FileChecker.CreateFile(); using (StreamReader sr = new StreamReader(FileChecker.filepath)) { using (JsonReader reader = new JsonTextReader(sr)) { messages = jsonSerializer.Deserialize <List <MessageHandler> >(reader); if (messages is null) { messages = new List <MessageHandler>(); } if (messages.Count > 0 && messages[messages.Count - 1].MessageCode != "-1") { messages.Add(new MessageHandler() { MessageCode = "-1", MessageType = MessageTypes.NORMAL }); } return(messages); } } }
/// <summary> /// Write all the messages in the message list at once /// </summary> public void WriteMessagesAtOnce() { FileChecker.CreateFile(); using (StreamWriter sw = new StreamWriter(FileChecker.filepath)) { using (JsonWriter writer = new JsonTextWriter(sw)) { if (messages is null) { messages = new List <MessageHandler>(); } messages.RemoveAll(x => x.MessageCode == "-1"); if (messages.Count > 0) { sw.Write(JsonConvert.SerializeObject(messages, Formatting.Indented)); } sw.Close(); } } }