Home > Back-end >  Why CsvHelper does not read data from CSV files
Why CsvHelper does not read data from CSV files

Time:10-13

I'm trying to create a windows service that detects if there are new CSV files in a folder and parse those files. CsvHelper seems to not be able to read the CSV file. All the parameters that I try to populate are empty.

Is there something wrong in the code? The GetField method does not return any values and If I print the parameters are all empty.

The path is correct and the csv files paths are also correct.

public class CSVBatch
{
    public string MaterialID { get; set; }
    public string MaterialName { get; set; }
    public string Location { get; set; }
    public string Quantity { get; set; }
    public string BatchID { get; set; }
    public string ProcessOrder { get; set; }
    public string Recipe { get; set; }

    public List<CSVRawMaterial> CSVRawMaterials { get; set; }
    public class CSVRawMaterial
    {
        public string MaterialID { get; set; }
        public string MaterialName { get; set; }
        public string Location { get; set; }
        public string Quantity { get; set; }
        public string BatchID { get; set; }
        public string ProcessOrder { get; set; }
        public string Recipe { get; set; }
    }
}

    protected override void OnStart(string[] args)
    {
        
        var folder = "C:\\BOM";
        FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(folder);
        var fw = fileSystemWatcher;
        fw.IncludeSubdirectories = true;
        fw.EnableRaisingEvents = true;
        fw.Created  = Newfileevent;
    }       
     
    static void Newfileevent(object sender, FileSystemEventArgs e)
    {
        string[] filePaths = Directory.GetFiles("C:\\BOM");

        foreach (string s in filePaths)
        {
            var config = new CsvConfiguration(CultureInfo.InvariantCulture)
            {
                Delimiter = ",",
                MissingFieldFound = null,
                TrimOptions = TrimOptions.Trim,
                HeaderValidated = null,
                HasHeaderRecord = true
            };
            using (var reader = new StringReader(s))
            using (var csv = new CsvReader(reader, config))
            {                   
                csv.Read();
                var batch = new CSVBatch
                {
                    MaterialID = csv.GetField<string>(0),
                    MaterialName = csv.GetField<string>(1),
                    Location = csv.GetField<string>(2),
                    Quantity = csv.GetField<string>(3),
                    BatchID = csv.GetField<string>(4),
                    ProcessOrder = csv.GetField<string>(5),
                    Recipe = csv.GetField<string>(6)
                };

                csv.Read();
                var rawMaterials = new List<CSVRawMaterial>();

                while (csv.Read())
                {
                    var rawmaterial = new CSVRawMaterial
                    {
                        MaterialID = csv.GetField<string>(0),
                        MaterialName = csv.GetField<string>(1),
                        Location = csv.GetField<string>(2),
                        Quantity = csv.GetField<string>(3)
                    };

                    rawMaterials.Add(rawmaterial);
                }

                batch.CSVRawMaterials = rawMaterials;
            }
        }

CSV File:

CSV File

CodePudding user response:

You have 2 issues.

  1. You are using StringReader instead of StreamReader. It should be:
using (var reader = new StreamReader(s))
  1. If you have a header row, you also have to specifically read the header when reading by hand.
using (var reader = new StreamReader(s))
using (var csv = new CsvReader(reader, config))
{                   
    csv.Read();
    csv.ReadHeader();

    csv.Read();
    batch = new CSVBatch
    {
  • Related