Search code examples
c#directorydirectoryinfogetdirectories

C# How to GetDirectories without 8.3 format names


I'm trying to figure out the best way to get a list of directories using a wildcard that doesn't include 8.3 format names. I'm trying to get all the directories using a wildcard pattern like "3626*". The problem is both 'Directory.GetDirectoriesandDirectoryInfo.GetDirectories` include long file names and 8.3 format names. Thus I get entries I don't want. For example with the above wild card I get both "3626 Burnt Chimney" and "3689 Lavista". You can see the same behavior using a command prompt and the command "dir 3626*". This is on Windows 7 32 bit. How can I get only long file names to return?


Solution

  • Perform the filter after you retrieve the directories, e.g,

    var files = new DirectoryInfo(@"C:\Path\")
                       .GetDirectories()
                       .Select(f => f.Name)
                       .Where(name => name.StartsWith("3626"));