How do I retrieve all filenames in a directory?
问题 How do I retrieve all filenames matching a pattern in a directory? I tried this but it returns the full path instead of the filename. Directory.GetFiles (path, "*.txt") Do I have to manually crop the directory path off of the result? It's easy but maybe there is an even simpler solution :) 回答1: foreach (string s in Directory.GetFiles(path, "*.txt").Select(Path.GetFileName)) Console.WriteLine(s); 回答2: Assuming you're using C#, the DirectoryInfo class will be of more use to you: DirectoryInfo