public async void CreateAllResultsFiles(int meetId, string folderPath) { if (folderPath.Last() != '\\') { folderPath += "\\"; } // Before building the results file delete all existing results files DeleteAllResultsFiles(folderPath, ".lff"); EventsController eventContoller = new EventsController(); AthleteController athleteController = new AthleteController(); EntriesController entriesController = new EntriesController(); MarksController markController = new MarksController(); List <ResultsHeader> resultsHeader = new List <ResultsHeader>(); var eventsList = eventContoller.GetEventsByMeetId(meetId); foreach (Event item in eventsList) { resultsHeader.Add(new ResultsHeader { EventId = item.EventId, EventNum = item.EventNum, RoundNum = item.FlightNum, FlightNum = item.FlightNum, EventName = item.EventName, MeasurementSystem = item.MeasurementType }); } StringBuilder headerText = new StringBuilder(); StringBuilder eventFileName = new StringBuilder(); foreach (ResultsHeader item in resultsHeader) { // Build the file name for the event results file eventFileName.Append( String.Concat( folderPath, item.EventNum.ToString().Length < 3 ? item.EventNum.ToString().PadLeft(3, '0') : item.EventNum.ToString(), "-", item.RoundNum.ToString(), "-", item.FlightNum.ToString(), ".lff")); // Adds the header to each file using (StreamWriter writer = new StreamWriter(eventFileName.ToString(), true)) { headerText.Append(String.Concat( item.EventNum.ToString(), ",", item.RoundNum.ToString(), ",", item.FlightNum.ToString(), ",", item.EventName, ",", item.MeasurementSystem, Environment.NewLine)); await writer.WriteAsync(headerText.ToString()); writer.Close(); headerText.Clear(); } await AddAthleteResults( eventFileName.ToString(), item.EventId, athleteController, entriesController, markController); // Clear the StringBuilder for the next file name eventFileName.Clear(); } }