0

I want to read in the content of multiple csv files in a list and use this list to split the contents in a datagridview. The problem is that with this code I only get the filenames but not the content of the files. How do I read in the content?

Below you can see the code.

public List<string> GetFileList(string strFilePath, bool SubFolders)
{
    List<string> FileArray = new List<string>();

    var fileContent = string.Empty;
    var filePath = string.Empty;

    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
        openFileDialog.InitialDirectory = "C:\\Users\\blabla\\Desktop\\folder\\files";
        openFileDialog.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
        openFileDialog.FilterIndex = 2;
        openFileDialog.RestoreDirectory = true;
        openFileDialog.Multiselect = true;

        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            try
            {
                string[] Files = System.IO.Directory.GetFiles(strFilePath);
                string[] Folders = System.IO.Directory.GetDirectories(strFilePath);

                for (int i = 0; i < Files.Length; i++)
                {
                    FileArray.Add(Files[i].ToString());
                }

                if (SubFolders == true)
                {
                    for (int i = 0; i < Files.Length; i++)
                    {
                        FileArray.AddRange(GetFileList(Folders[i], SubFolders));
                    }
                }
            }
            catch (Exception Ex)
            {
                throw (Ex);
            }
            return FileArray;
        }
        return FileArray;
    }
}

I expected to use the contents in FileArray for a datagridview, but I only get the filenames. Thank you very much for your help!

Roberto Pegoraro
  • 1,313
  • 2
  • 16
  • 31
  • Possible duplicate of [Reading CSV files using C#](https://stackoverflow.com/questions/3507498/reading-csv-files-using-c-sharp) – Ethan Shafer Jul 23 '19 at 11:34

2 Answers2

0

You are getting only file names because you are reading only file names :)

You need to use

string[] lines = System.IO.File.ReadAllLines(Files[i].ToString());
FileArray.AddRange(lines)

in your program.

MSDN states:

System.IO.Directory.GetFiles

Returns the names of files that meet

specified criteria.

So you need to open each and every file, read all content from it and add it to the return variable.

Community
  • 1
  • 1
elrado
  • 4,960
  • 1
  • 17
  • 15
0

Are you able to import namespace Microsoft.VisualBasic.FileIO? If yes, then you might have a look at the TextFieldParser class (see microsoft documentation and example). This is a very comfortable class to import CSV files and read them line by line. In a loop you could then fill your target collection with the corresponding data.

Grimm
  • 690
  • 8
  • 17
  • If you're reading CSV files on a line-by-line basis then you're doing it wrong. CSV files are a streaming protocol that should be read with a state machine. Have a read through [Common Format and MIME Type for Comma-Separated Values (CSV) Files](https://tools.ietf.org/html/rfc4180) – AlwaysLearning Jul 23 '19 at 11:52
  • Where exactly in your linked text does it say that CSV is a streaming protocol? How will a state machine read the CSV file at the end of the day, if not line by line? – Grimm Jul 23 '19 at 12:18
  • 1
    Have a close look at example 6 in "2. Definition of the CSV Format." Line breaks can appear in the middle of fields. If you're reading a CSV file line-by-line you'll break fields that contain a line break. A compliant CSV reader will be processing the file one character at a time. – AlwaysLearning Jul 23 '19 at 13:34
  • Ah, ok, then we talked a bit past each other :-) Based on the RFC you're right of course. I meant you can loop line by line through TextFieldParser's output since it can get along with line breaks within fields (if they're correctly enclosed in double quotes). That's what I meant by calling it a comfortable class. But again, your hint is absolutely right that on the lowest level the file should be read character wise (So my second question is ultimately wrong). – Grimm Jul 23 '19 at 14:20