83 lines
2.3 KiB
Plaintext
83 lines
2.3 KiB
Plaintext
using DTS.Common.Import.ImportOptions;
|
|
using Microsoft.VisualBasic.FileIO;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DTS.Common.Import
|
|
{
|
|
public class CSVFile
|
|
{
|
|
private readonly string _filename;
|
|
public CSVFile(string filename)
|
|
{
|
|
_filename = filename;
|
|
}
|
|
public bool IsInUse()
|
|
{
|
|
|
|
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)
|
|
{
|
|
using (var parser = new TextFieldParser(filename, Encoding.GetEncoding(csvImportOptions.Encoding), true))
|
|
{
|
|
parser.TextFieldType = FieldType.Delimited;
|
|
parser.SetDelimiters(csvImportOptions.FieldSeparator);
|
|
var tokens = parser.ReadFields();
|
|
|
|
|
|
if (tokens != null && tokens[0] == "Version")
|
|
{
|
|
return true;
|
|
}
|
|
|
|
}
|
|
return false;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
}
|