dasun dasun - 1 month ago 10
C# Question

how to select a csv file automatically from a specified path in c#

I am a newbie to programming so i am hoping support for my problems from all my friends
This is the problem
I have used the loation as 2DB to read a csv file.It works well when I give the file name also But I want to give only location and program needs to select the file

string path = "G:\kaash\2DB\";

string[] row_text = System.IO.File.ReadAllLines(""+path+", *.csv");
string[] data_col = null;

Answer

This code will help you to find all .csv files as a list of strings from the specified directory.

string path = @"G:\kaash\2DB\";
List<string> csvIn2DB = System.IO.Directory.GetFiles(path, "*.csv", SearchOption.TopDirectoryOnly).ToList();

By modifying the search pattern you can locate your files more specifically. You can change the SearchOption to AllDirectories if you want to extend the search to inner folders of the specified directory.

Let you need to get all your .csv file names follows the pattern "FILECSV_xxxxxx.csv", then the search pattern will be like this: FILECSV_*.csv