question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked karenpayneoregon edited

C# filter file extensions

Hello,

I have a folder with many files.
The files are structured as follows.
Name001.product
Name002.product
Name003.product
Name004.product
Name00n.product

Additionally
Name101.product.prototype
Name102.product.prototype
Name103.product.prototype.
Name104.product.prototype
Name10n.product.prototype


Is there a file filter that gives me only the files without prototype in a list?

*.product
delivers all.

Which query would work well here? I need only .product

not .product*

dotnet-csharpwindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered karenpayneoregon edited

I created files as per

 string[] names = {
     "Name001.product",
     "Name002.product",
     "Name003.product",
     "Name101.product.prototype",
     "Name102.product.prototype",
     "Name103.product.prototype",
 };
    
 foreach (var name in names)
 {
     File.WriteAllText(name,"");
 }

Then in another event

 Directory.GetFiles(
     AppDomain.CurrentDomain.BaseDirectory,"*.product")
     .ToList()
     .ForEach(Console.WriteLine);
    
 Console.WriteLine();
    
 Directory.GetFiles(
     AppDomain.CurrentDomain.BaseDirectory, 
     "*.product.prototype")
     .ToList()
     .ForEach(Console.WriteLine);


Outputs

C:\Dotnetland\VS2019\ForumQuestion\Demo\bin\Debug\Name001.product
C:\Dotnetland\VS2019\ForumQuestion\Demo\bin\Debug\Name002.product
C:\Dotnetland\VS2019\ForumQuestion\Demo\bin\Debug\Name003.product

C:\Dotnetland\VS2019\ForumQuestion\Demo\bin\Debug\Name101.product.prototype
C:\Dotnetland\VS2019\ForumQuestion\Demo\bin\Debug\Name102.product.prototype
C:\Dotnetland\VS2019\ForumQuestion\Demo\bin\Debug\Name103.product.prototype

I ran the above code with .NET Core 5 and .NET Framework 4.8, both yield the same output.

Another example with a different pattern and predicate.

 Directory.GetFiles(
         AppDomain.CurrentDomain.BaseDirectory, "Name*.*")
     .Where(fileName => !fileName.EndsWith("prototype"))
     .ToList()
     .ForEach(Console.WriteLine);


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.