public DataTable LoadDataTableFromCSV(string csvPath, char?separator = null) { if (MyLoadDataTableFromCSV != null) { return(MyLoadDataTableFromCSV(csvPath, separator)); } DataTable result = null; bool isHeader = true; Regex regexp = null; List <string> languages = new List <string>(); string[] lines = null; try { lines = File.ReadAllLines(csvPath, DefaultEncoding); } catch { //Try by copying the file... string newPath = FileHelper.GetTempUniqueFileName(csvPath); File.Copy(csvPath, newPath); lines = File.ReadAllLines(newPath, DefaultEncoding); FileHelper.PurgeTempApplicationDirectory(); } foreach (string line in lines) { if (string.IsNullOrWhiteSpace(line)) { continue; } if (regexp == null) { string exp = "(?<=^|,)(\"(?:[^\"]|\"\")*\"|[^,]*)"; if (separator == null) { //use the first line to determine the separator between , and ; separator = ','; if (line.Split(';').Length > line.Split(',').Length) { separator = ';'; } } if (separator != ',') { exp = exp.Replace(',', separator.Value); } regexp = new Regex(exp); } MatchCollection collection = regexp.Matches(line); if (isHeader) { result = new DataTable(); for (int i = 0; i < collection.Count; i++) { result.Columns.Add(new DataColumn(collection[i].Value, typeof(string))); } isHeader = false; } else { var row = result.Rows.Add(); for (int i = 0; i < collection.Count && i < result.Columns.Count; i++) { row[i] = collection[i].Value; } } } return(result); }