Reputation:
Currently i am reading a specific .csv and with each of them one by one need help i am a rookie coder.
for this Example i need to Read all the .csv files which is present in the folder
my existing code
class Program
{
static void Main(string[] args)
{
string csv_file_path = @"C:\Sample files\L.csv";
DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);
}
}
Upvotes: 0
Views: 215
Reputation: 436
I think this works for you
List<string> directory = Directory.EnumerateFiles(folderPath, "*.csv");
if (directory.Count() != 0)
{
foreach (string filePath in directory)
{
DataTable csvDataTable = GetDataTablefromCSV(filePath);
}
}
The csv function
public static DataTable GetDataTablefromCSV(string filePath)
{
try
{
StreamReader _streamReader = new StreamReader(filePath);
string[] headers = _streamReader.ReadLine().Split(',');
DataTable dataTable = new DataTable();
foreach (string header in headers)
{
dataTable.Columns.Add(header);
}
while (!_streamReader.EndOfStream)
{
string[] rows = Regex.Split(_streamReader.ReadLine(), ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
DataRow dataRow = dataTable.NewRow();
for (int i = 0; i < headers.Length; i++)
{
dataRow[i] = rows[i];
}
dataTable.Rows.Add(dataRow);
}
return dataTable;
}
catch(Exception ex)
{
return null;
}
}
Upvotes: 1
Reputation: 34421
I modified code to work with multiple csv files. See changes below
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string csv_file_path = @"C:\Sample files";
DataTable csvData = new DataTable();
string[] files = Directory.GetFiles(csv_file_path, "*.csv");
Boolean firstFile = true;
foreach (string file in files)
{
GetDataTabletFromCSVFile(file, csvData, firstFile);
firstFile = false;
}
}
private static void GetDataTabletFromCSVFile(string csv_file_path, DataTable csvData, Boolean firstFile)
{
try
{
int lineCount = 0;
using (StreamReader csvReader = new StreamReader(csv_file_path))
{
string line = "";
while ((line = csvReader.ReadLine()) != null)
{
string[] colFields = line.Split(new char[] { ',' }).ToArray();
if (++lineCount == 1)
{
if (firstFile)
//read column names
foreach (string column in colFields)
csvData.Columns.Add(column, typeof(string));
}
else
{
//Making empty value as null
for (int i = 0; i < colFields.Length; i++)
if (colFields[i] == "")
colFields[i] = null;
csvData.Rows.Add(colFields);
}
}
}
}
catch (Exception) { }
}
}
}
Upvotes: 0
Reputation: 36
I'd recommend looking into using a DirectoryInfo
to find all of the files in the given directory. A DirectoryInfo
is essentially an object that contains information about all files and directories within a directory. You can loop through all the files in that directory using the DirectoryInfo
's GetFiles
function and look for CSV files. From there it's just a matter of doing whatever it is you intend to do with each file.
Directory Info Documentation Link
Also, I noticed you're using an absolute path here:
string csv_file_path = @"C:\Sample files\L.csv";
This is typically considered to be bad practice, and for good reason. What happens if this program is run on any machine other than the one you're programming it on? It's probably better to use relative paths rather than absolute paths for this kind of thing so that your code can work more universally. Relative paths are, as they sound, paths that are relative to the current file location.
Here is an article about relative and absolute paths for you in case you wish to learn more about that. A bit of Googling can also help if you're still confused on that.
Upvotes: 1