It's quite easy, actually. You can use the System.IO.Directory
class in conjunction with System.IO.Path
. Something like (using LINQ makes it even easier):
var allFilenames = Directory.EnumerateFiles(path).Select(p => Path.GetFileName(p));
// Get all filenames that have a .txt extension, excluding the extension
var candidates = allFilenames.Where(fn => Path.GetExtension(fn) == ".txt")
.Select(fn => Path.GetFileNameWithoutExtension(fn));
There are many variations on this technique too, of course. Some of the other answers are simpler if your filter is simpler. This one has the advantage of the delayed enumeration (if that matters) and more flexible filtering at the expense of more code.