To elaborate further, the reason you are getting the ArgumentException
is because the second parameter is not valid.
The overload of the Directory.GetFiles
method that you are using is expecting a string path
, string searchPattern
, and a SearchOption searchOption
.
The searchPattern
is not a regular expression. You can only use a combination of * and ? characters.
From the documentation:
The search string to match against the names of files in path. This
parameter can contain a combination of valid literal path and wildcard
(* and ?) characters, but it doesn't support regular expressions.
Alternative implementation using the System.Linq
Concat
extension method:
string[] mainFileNames = Directory.GetFiles(@"/Some/Path", "*MAIN.txt", SearchOption.TopDirectoryOnly);
string[] contFileNames = Directory.GetFiles(@"/Some/Path", "*CONT.txt", SearchOption.TopDirectoryOnly);
string[] allFileNames = mainFileNames.Concat(contFileNames).ToArray();