c# Windows APP Hi! I am developing a windows form application that will perform
ID: 3918539 • Letter: C
Question
c# Windows APP
Hi! I am developing a windows form application that will perform as a tool for data analyzation. I have two checkedlistbox's , two datetimepicker's, a data grid view, and a series chart.
I am having difficulty filterig my list of paths in "fileDirectores" I would like to filter by file names(specific characters in the file names "like '1' or 'NG'") in the selectedPath of the fileDirectories:
if (checkedListBox1.GetItemCheckState(0) == CheckState.Checked)
{
if (checkedListBox2.GetItemCheckState(0) == CheckState.Checked)
{
fileDirectories.Add(@"C:Users nooluadeDesktop esult file path");
}
else if (checkedListBox2.GetItemCheckState(1) == CheckState.Checked)
{
.................
}
...........................................................................
foreach (string selectedPath in fileDirectories)
{
string[] level1 = Directory.GetFiles(selectedPath, "*.csv", SearchOption.AllDirectories);
foreach (string level2 in level1)
{
//I need an example here on how to filter the files in SelectedPath by key characters and letters and add to add to a new list which will use later in the application.
}
Explanation / Answer
Instead of filtering in the foreach, get filtered file names in the level using filter with GetFile(). Second parameter passed to GetFiles() is a regular expression to match file names which could be modified to get the results based on your requirements. E.g. if you want to get all .csv files which contains ‘NG’ then call GetFile() as below:
string[] level1 = Directory.GetFiles(selectedPath, "*” + “NG” + “*.csv", SearchOption.AllDirectories);
If you want all the files containg ‘NG’ and not just .csv files then function call will be as below:
string[] level1 = Directory.GetFiles(selectedPath, "*” + “NG” + “*", SearchOption.AllDirectories);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.