The overload of the string compare method you're using does not accept a pattern to compare to, instead a second string to compare the first to. Which means if you have a file "fooBar.png" in your directory, your eventually comparing it's extension (so ".png") to "*.png", which is not the same.
You also said you want to get all file names that end with one of a number of specified extensions, but your using .All(...)
, which only returns true if all items inside the enumeration match the given expression. So
All(x=> string.Compare(Path.GetExtension(x),"*.pdf", StringComparison.CurrentCultureIgnoreCase) == 0)
would only return true if all files inside the directory were pdf files.
There is also not necessarily a problem, but something sub-optimal in your code: Your reading the same content from disk multiple times, which is as said, sub-optimal.
That being said, here is some updated code to fix your problem:
var acceptedFileTypes = new List<string>
{
".png",
".pdf",
...
};
// Get all files in the specified directory
var filesInDirectory = Directory.GetFiles(directory);
// Only select those with accepted file extensions
// Explicit type for better understanding
List<string> supportedFiles = filesInDirectory.Where(
file => acceptedFileTypes.Contains(Path.GetExtension(file))
).ToList();
// Do something with the supportedFiles
// e.g print them to the console:
foreach (var file in supportedFiles)
{
Console.WriteLine($"Found supported file: {file}");
}
You can do whatever you want with this, put it in a method and swap acceptedFileTypes
for a static member, or put this in a static class of its own etc.
As well you can add new file types easily by appending the List