I have a foreach statement that is part this function:
try
{
string pathToFiles = sourceTB.Text;
DirectoryInfo dirInfo = new DirectoryInfo(pathToFiles);
int i = 0;
foreach (var files in dirInfo.EnumerateFiles())
{
//Do stuff here
}
MessageBox.Show("Process Successful", "Completed");
}
catch
{
MessageBox.Show("Process failed", "Failed");
}
I would like to be able to code it so that it will read the entire contents of the selected folder (this is working fine), but I am trying to make it so it will skip any files with a certain file extension.
For Example if every file in the folder was a .txt file and I wanted to leave out the .jpg files.
I have tried a few ways to achieve this such as:
var allFilesFromFolder = Directory.GetFiles(pathToFiles);
var filesToExclude = Directory.GetFiles(pathToFiles, "*.jpg");
var filetoInclude = allFilesFromFolder.Except(filesToExclude);
and this:
var files = Directory.GetFiles(pathToFiles).Where(name => !name.EndsWith(".jpg"));
but both of these bring errors into to the code and wont fit in the foreach loop when cycles through the files.
Is there any way to achieve this?