RPS
RPS

Reputation: 1401

Checking a folder for a file

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

Answers (3)

Michael Edenfield
Michael Edenfield

Reputation: 28338

You'll want to use the Directory class, specifically: Directory.EnumerateFiles(), which accepts a search wildcard pattern.

Upvotes: 0

Talljoe
Talljoe

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

Non
Non

Reputation: 2006

You could use FileInfo and DirectoryInfo from System.IO.

Upvotes: 0

Related Questions