public async Task <string> WriteTextAsync(string filePath, DrugDetails drugDetails) { bool isAddedOrAppended = false; string text = drugDetails.DrugName + " " + drugDetails.DrugExpiredOn + drugDetails.LotNo + Environment.NewLine; string SAMPLE_KEY = "gCjK+DZ/GCYbKIGiAt1qCA=="; string SAMPLE_IV = "47l5QsSe1POo31adQ/u7nQ=="; var key = Encoding.UTF8.GetBytes(SAMPLE_KEY); var iv = Encoding.UTF8.GetBytes(SAMPLE_IV); Array.Resize(ref key, 128 / 8); Array.Resize(ref iv, 128 / 8); if (File.Exists(filePath) && File.ReadLines(filePath).Any(line => line.Length > 0)) { isAddedOrAppended = await StreamHelper.AppendStringToFile(filePath, text, key, iv); } else { isAddedOrAppended = await StreamHelper.WriteStringToFile(filePath, text, key, iv); } return(isAddedOrAppended ? "success" : "failure"); }
public async Task <string> WriteTextAsync(string filePath, DrugDetails drugDetails) { string text = drugDetails.DrugName + " " + drugDetails.DrugExpiredOn + Environment.NewLine; byte[] encodedText = Encoding.UTF8.GetBytes(text); using (FileStream sourceStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true)) { string returnValue = System.Convert.ToBase64String(encodedText); byte[] info = new UTF8Encoding(true).GetBytes(returnValue); await sourceStream.WriteAsync(info, 0, info.Length); return("success"); }; }