C# 如何获取没有 8.3 格式名称的目录

C# How to GetDirectories without 8.3 format names

我正在尝试找出使用不包含 8.3 格式名称的通配符获取目录列表的最佳方法。我正在尝试使用 "3626*" 等通配符模式获取所有目录。问题是“Directory.GetDirectoriesandDirectoryInfo.GetDirectories”都包含长文件名和 8.3 格式名称。因此我得到了我不想要的条目。例如,使用上面的通配符,我得到 "3626 Burnt Chimney""3689 Lavista"。您可以使用命令提示符和命令 "dir 3626*" 看到相同的行为。这是 Windows 7 32 位。我怎样才能只得到长文件名到 return?

您可以在此处找到更多信息:List files in folder which match pattern

RagtimeWilly 的评论也是我想推荐的。

有关 8.3 名称的详细解释,请参阅 http://en.wikipedia.org/wiki/8.3_filename

我只是写了一个小例子,也许你可以以此为起点:

using System.Text.RegularExpressions;
...

Regex re83 = new Regex(@"^[^.]{1,8}(\.[^.]{0,3})?$");

DirectoryInfo directoryInfo = new DirectoryInfo("C:\windows");

foreach (string no83 in directoryInfo.GetDirectories("i*").Select(di => di.Name).Where(n => !re83.IsMatch(n)))
{
    Console.WriteLine(no83);
}

在网络上搜索,您发现了一些用于匹配 8.3 名称的其他正则表达式,有些更复杂一些。 8.3 中不允许使用某些字符。名称,由那些处理。但是您可能只是想过滤掉一些不需要的路径名。

检索目录后执行过滤,例如

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