-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIOHelpers.cs
32 lines (28 loc) · 938 Bytes
/
IOHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using CsvHelper.Configuration;
using CsvHelper;
using System.Globalization;
using System.Text;
namespace EasyCBR.IO;
internal class IOHelpers
{
internal static List<T> ReadCsvFile<T>(string path, bool hasHeaders = true)
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = hasHeaders
};
using var reader = new StreamReader(path, Encoding.Default);
using var csv = new CsvReader(reader, config);
return csv.GetRecords<T>().ToList();
}
internal static void InsertRecordToCsvFile<T>(T newItem, string path, bool hasHeaders = true)
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = hasHeaders
};
using var writer = new StreamWriter(path, true);
using var csv = new CsvWriter(writer, config);
csv.WriteRecord(newItem);
}
}