/// <summary> /// Init the specified filePath and hasHeaderRow. /// </summary> /// <param name="filePath">string</param> /// <param name="hasHeaderRow">If set to <c>true</c> has header row.</param> public static void Init(string filePath, bool hasHeaderRow = true) { theCSV = InMemoryCSVFile.ReadCSVFile(filePath); for (int i = hasHeaderRow ? 1 : 0; i < theCSV.LineCount; i++) { theCSV = InMemoryCSVFile.ReadCSVFile(filePath); } }
/// <summary> /// Reads the CSV File. /// </summary> /// <returns>InMemoryCSVFile</returns> /// <param name="filePath">string</param> /// <param name="delimiter">char</param> /// <param name="hasHeaderRow">bool</param> public static InMemoryCSVFile ReadCSVFile(string filePath, bool hasHeaderRow = false, char delimiter = ',') { int maxColumnLength = 0; List <string[]> ret = new List <string[]>(); if (File.Exists(filePath)) { try { StreamReader fs = new StreamReader(filePath); if (hasHeaderRow) { ret.Add(fs.ReadLine().Split(delimiter)); } while (!fs.EndOfStream) { string [] line = fs.ReadLine().Split(delimiter); if (line.Length > maxColumnLength) { maxColumnLength = line.Length; } if (line.Length > 1)//skip over blank lines { ret.Add(line); } } } catch (IOException ex) { Console.WriteLine(ex.Message); } } else { throw new IOException($"File '{filePath}' was not found. Unable to load CSV File."); } InMemoryCSVFile csvFile = new InMemoryCSVFile(filePath, maxColumnLength, hasHeaderRow, ret); return(csvFile); }