public async Task <TDataObject> Load <TDataObject>(string filePath) { try { byte[] bufferToRead; using (var readFile = new FileStream(filePath, FileMode.Open)) { try { var fileLength = Convert.ToInt32(readFile.Length); bufferToRead = new byte[fileLength]; await readFile.ReadAsync(bufferToRead, 0, fileLength); } catch (OverflowException) { throw new FileTooBigException(filePath, int.MaxValue); } } var modelJson = BufferWorker.ToString(bufferToRead); return(ParseString <TDataObject>(modelJson)); } catch (FileNotFoundException) { throw new FileNotFoundException($"File \"{filePath}\" does not exist"); } }
public async Task Save(object objectToBeSaved, string filePath) { var modelJson = JsonConvert.SerializeObject(objectToBeSaved, Formatting.Indented); if (File.Exists(filePath)) { File.Delete(filePath); } var bufferToWrite = BufferWorker.ToBuffer(modelJson); using (var saveFile = new FileStream(filePath, FileMode.CreateNew)) { await saveFile.WriteAsync(bufferToWrite, 0, bufferToWrite.Length); } }