i found several questions on stack overflow directory.getfiles() in of cases, explain how use find specific extension or set of files through multiple criteria. in case, want search pattern directory.getfiles() using regular expressions, return of files of directory set i'm specifying. mean not declare set want difference. example, if want of files of directory not htmls. notice that, i',m know achieve in way:
var filteredfiles = directory .getfiles(path, "*.*") .where(file => !file.tolower().endswith("html"))) .tolist();
but not reusable solution, if later want filter kind of file have change code adding || condition. i'm looking allows me create regex, consist in files don't want , pass directory.getfiles(). so, if want filter more extensions later, changing regex.
you don't need regex if want filter extension(s):
// example field or property in class private hashset<string> extensionblacklist { get; } = new hashset<string>(stringcomparer.invariantcultureignorecase) { ".html", ".htm" }; // ... var filteredfiles = directory.enumeratefiles(path, "*.*") .where(fn => !extensionblacklist.contains(system.io.path.getextension(fn))) .tolist();
Comments
Post a Comment