private void SetStockItem(string key, object value) { List <StockItem> items = GetItemList(); using (FileStream stockFile = File.Create(FilePath)) { if (items == null) { items = new List <StockItem>(); } BinaryFormatter formatter = new BinaryFormatter(); StockItem item = items?.Where(i => i.Key == key).FirstOrDefault(); if (item != null) { item.Value = value; } else { items.Add(new StockItem(key, value)); } formatter.Serialize(stockFile, items); } }