private ItemsCollection Load() { using (RijndaelManaged r = new RijndaelManaged()) { SetCryptoKeys(r); ICryptoTransform decryptor = r.CreateDecryptor(r.Key, r.IV); using (Stream fileStream = File.Open("words.json", FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read)) { if (fileStream.Length == 0) { return(new ItemsCollection()); } using (CryptoStream csDecrypt = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Read)) { using (StreamReader streamReader = new StreamReader(csDecrypt)) { string content = streamReader.ReadToEnd(); string jsonString = string.IsNullOrWhiteSpace(content) ? "[]" : content; ItemsCollection items = JsonConvert.DeserializeObject <ItemsCollection>(jsonString); return(items); } } } } }
public void Add(string voc, string translation, string hint) { ItemsCollection items = Load(); items.Add(voc, translation); Save(items); }
public void Remove(string voc) { ItemsCollection items = Load(); if (items[voc] != null) { items.Remove(voc); } Save(items); }
private ItemsCollection Load() { using (Stream fileStream = File.Open("words.json", FileMode.OpenOrCreate)) { using (StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8)) { string content = streamReader.ReadToEnd(); string jsonString = string.IsNullOrWhiteSpace(content) ? "[]" : content; ItemsCollection items = JsonConvert.DeserializeObject <ItemsCollection>(jsonString); return(items); } } }
private void Save(ItemsCollection items) { string jsonString = JsonConvert.SerializeObject(items); byte[] buffer = Encoding.UTF8.GetBytes(jsonString); //buffer = Encoding.ASCII.GetBytes(jsonString); int count = 1; Stream fileStream = File.Open("words.json", System.IO.FileMode.OpenOrCreate); for (int offset = 0; offset < buffer.Length; offset++) { fileStream.Write(buffer, offset, count); } fileStream.Close(); }
private void Save(ItemsCollection items) { string jsonString = JsonConvert.SerializeObject(items); using (RijndaelManaged r = new RijndaelManaged()) { SetCryptoKeys(r); ICryptoTransform encryptor = r.CreateEncryptor(r.Key, r.IV); Stream fileStream = File.Open("words.json", System.IO.FileMode.OpenOrCreate); using (CryptoStream csEncrypt = new CryptoStream(fileStream, encryptor, CryptoStreamMode.Write)) { using (StreamWriter writer = new StreamWriter(csEncrypt)) { writer.Write(jsonString); } } fileStream.Close(); } }