In C# how can I search through a Folder and its Subfolders to find files that match a string value. My string value could be "ABC123" and a matching file might be ABC123_200522.tif. Can an Array collect these?
You're looking for the Directory.GetFiles
method:
Directory.GetFiles(path, "*" + search + "*", SearchOption.AllDirectories)
If the matching requirements are simple, try:
string[] matchingFiles = System.IO.Directory.GetFiles( path, "*ABC123*" );
If they require something more complicated, you can use regular expressions (and LINQ):
string[] allFiles = System.IO.Directory.GetFiles( path, "*" );
RegEx rule = new RegEx( "ABC[0-9]{3}" );
string[] matchingFiles = allFiles.Where( fn => rule.Match( fn ).Success )
.ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With