0

I am writing a console application that reads multiple CSV files from the specified folder using SmartXLS library. I am able to read from a single file but unable to figure out how to read multiple files. Kindly, help me with this.

public void GetData()
        {

            int count = 0;

            DeskTokens = new List<Token>();

            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string path = Path.Combine(directory, @"C:\projects\Product_Usage_Year.csv");

            SmartXLS.WorkBook WB = new WorkBook();
            WB.readCSV(path);

            DataTable dt = WB.ExportDataTable();

            string CurrentType = string.Empty;
            string CurrentCategory = string.Empty;

            DataRow dr;
            for (int i = 1; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                var tkn = new Token();

                tkn.Product_name = dr[0].ToString();
                tkn.Product_Version = dr[1].ToString();
                tkn.Userid = dr[2].ToString();
                tkn.User_name = dr[3].ToString();

                DeskTokens.Add(tkn);
                count++;
                Console.WriteLine("Read : " + count);

                Console.WriteLine("    Reading : " + tkn.Product_name + "," + tkn.Product_Version + "," + tkn.Userid + "," + tkn.User_name);

            }
        }
Rev4
  • 45
  • 1
  • 11

2 Answers2

4

Below, "path" is the directory in which all your CSV files reside.

var files = Directory.EnumerateFiles("path", "*.csv");
foreach (string file in files)
{
    using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
    {
        // Use the file stream to read data.
    }
}
Sach
  • 10,091
  • 8
  • 47
  • 84
  • 2
    You can use this also with a library called CSVHelper to parse your CSV data – DOMZE Aug 18 '17 at 18:52
  • 1
    You're welcome! And just an FYI, if you have to work with a large number of files `Directory.EnumerateFiles()` is faster than `Directory.GetFiles()`. [Reason here](https://stackoverflow.com/a/5669635/302248). – Sach Aug 18 '17 at 19:53
  • Sure. I will take your suggestion. :) @Sach – Rev4 Aug 18 '17 at 20:14
  • Additionally, May I know how can I modify the code to read all the csv files with different column data from a single folder? Could you suggest me any resources that I could look for? @Sach. – Rev4 Aug 24 '17 at 13:12
  • @Sach I would like to ask you: I have a folder that contains multiple subfolders. inside those subfolders, there are multiple CSV files. How can I access each subfolder and open one by one the CSV files in order to perform the desired operation ? – noruk Mar 06 '21 at 16:29
0

Use Directory.GetFiles:

// Gets only .csv files 
string[] csvFiles = Directory.GetFiles(directoryPath, "*.csv");

and write a loop like this:

foreach(file in csvFiles)
{
   getData(file);
   // ...
}

And also you should take a path parameter in getData method:

public void GetData(string path)
        {

            int count = 0;

            DeskTokens = new List<Token>();

            SmartXLS.WorkBook WB = new WorkBook();
            WB.readCSV(path);

            DataTable dt = WB.ExportDataTable();

            string CurrentType = string.Empty;
            string CurrentCategory = string.Empty;

            DataRow dr;
            for (int i = 1; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                var tkn = new Token();

                tkn.Product_name = dr[0].ToString();
                tkn.Product_Version = dr[1].ToString();
                tkn.Userid = dr[2].ToString();
                tkn.User_name = dr[3].ToString();

                DeskTokens.Add(tkn);
                count++;
                Console.WriteLine("Read : " + count);

                Console.WriteLine("    Reading : " + tkn.Product_name + "," + tkn.Product_Version + "," + tkn.Userid + "," + tkn.User_name);

            }
        }

I hope to be helpful for you:)

Ali Adlavaran
  • 3,697
  • 2
  • 23
  • 47
  • Hi. in my case I have a folder that contains multiple subfolders. inside those subfolders, there are multiple CSV files. How can I access each subfolder and open one by one the CSV files in order to perform the desired operation ? – noruk Mar 06 '21 at 16:28