-1

I have a C# program that needs to process CSV files. I have the program working fine when I specify just one file, but I tried to change it to process all CSV files in a specified file directory and it is no longer working correctly. It picks up the files but does not read them. This is the basic structure I'm trying to use:

string[] filePaths = Directory.GetFiles(@"C:\Users\XXXXX\Documents\", "*.csv");

    foreach (var file in filePaths)
    {
        StreamReader streamReader = new StreamReader(file);

        try
        {
             while (!streamReader.EndOfStream)
             {
                 string[] totalData = new string[System.IO.File.ReadAllLines(file).Length];
             }
        }
    }

pixelstars
  • 21
  • 4
  • _"It picks up the files but does not read them"_ What does that mean? You do realize that `new string[ReadAllLines().Length]` will create an empty array of string, right? This will be the same whether you process one file or all files. – 41686d6564 stands w. Palestine Sep 08 '20 at 21:41
  • 1
    You should rush to get a CSV processing library such as CSVHelper. – Ňɏssa Pøngjǣrdenlarp Sep 08 '20 at 21:43
  • Does this answer your question? [how to read all files inside particular folder](https://stackoverflow.com/questions/5840443/how-to-read-all-files-inside-particular-folder) – devlin carnate Sep 08 '20 at 21:52

1 Answers1

1

Your while loop is going to run forever because you are not advancing the StreamReader instance at all. You are not actually using the StreamReader to do anything.

If you just want to read all of the text in one shot, you can use File.ReadAllLines and not use a StreamReader.

If you want to read the files one line at a time, use a StreamReader.

Also, if you do use a StreamReader you should use it in conjuntion with 'using' to ensure it gets disposed when you're done with it.

mtreit
  • 789
  • 4
  • 6