Reputation: 1401
What is the best way to perform the following with c#:
Ex)
E03.chk
E01.chk
E02.chk
in this example, I would need to get E01.chk file name returned.
Upvotes: 1
Views: 146
Reputation: 28338
You'll want to use the Directory class, specifically: Directory.EnumerateFiles()
, which accepts a search wildcard pattern.
Upvotes: 0
Reputation: 14827
var regex = new Regex(@"^E\d\d$");
var file = Directory.GetFiles(path, "E??.chk")
.Where(f => regex.IsMatch(File.GetFileNameWithoutExtension(f)))
.OrderBy(f => f)
.FirstOrDefault();
Upvotes: 6