using DTS.Common.Import.ImportOptions; using System; using System.IO; using System.Linq; namespace DTS.Common.Import { public class CSVFile { private readonly string _filename; public CSVFile(string filename) { _filename = filename; } public static bool IsInUse(string filename) { if (!string.IsNullOrEmpty(filename)) { try { _ = File.ReadAllLines(filename); return false; } catch (IOException) { return true; } catch (Exception) { //We can supress this exception since we are interested in IOException only } } return true; } //FB 40598 check the format of csv and determine if is test setup or not public static bool IsCSVFileForTestSetupImport(string filename, CsvImportOptions csvImportOptions) { try { using (var csvReader = CsvUtil.CreateCsvReader(filename)) { var tokens = CsvUtil.ReadFields(csvReader); if (tokens != null && tokens[0] == "Version") { return true; } } } catch { // return false later } return false; } //FB 43815 Get the CSV file version public static int GetCsvVersion(string filename) { try { using (var csvReader = CsvUtil.CreateCsvReader(filename)) { var tokens = CsvUtil.ReadFields(csvReader); if (tokens != null && tokens[0] == "Version") { var tokens2 = CsvUtil.ReadFields(csvReader); if (tokens2 != null) { var version = Convert.ToInt32(tokens2[0]); return version; } } return 0; } } catch { return 0; } } public int LineCount { get { var count = 0; if (!string.IsNullOrEmpty(_filename)) { string[] lines; try { lines = File.ReadAllLines(_filename); return lines.Count(); } catch (Exception) { return count; } } return count; } } } }