问题描述
假设我已按名称对资源管理器中的文件列表进行排序,如下所示:
Let's say I've sorted a list of files in Explorer by name, like so:
2009-06-02-4.0.9.txt
2009-06-02-4.0.10.txt
2009-06-02-4.0.11.txt
2009-06-02-4.0.12.txt
2009-06-02-4.0.9.txt
2009-06-02-4.0.10.txt
2009-06-02-4.0.11.txt
2009-06-02-4.0.12.txt
我有一个 FileInfo 比较器,它按名称对 FileInfo 对象数组进行排序:
I have a FileInfo Comparer that sorts an array of FileInfo objects by name:
class FileInfoComparer : IComparer<FileInfo> {
public int Compare(FileInfo x, FileInfo y) {
return string.Compare(x.FullName,
y.FullName,
StringComparison.OrdinalIgnoreCase);
}
}
使用此比较器从上面对相同的文件列表进行排序会产生:
Sorting that same list of files from above using this Comparer yields:
2009-06-02-4.0.10.txt
2009-06-02-4.0.11.txt
2009-06-02-4.0.12.txt
2009-06-02-4.0.9.txt
2009-06-02-4.0.10.txt
2009-06-02-4.0.11.txt
2009-06-02-4.0.12.txt
2009-06-02-4.0.9.txt
这是有问题的,因为顺序非常重要.
which is problematic, as the order is extremely important.
我想有一种方法可以模拟 Windows 在 C# 代码中的操作,但我还没有找到方法.任何帮助表示赞赏!
I would imagine there's a way to mimic what Windows is doing in C# code, but I have yet to find a way. Any help is appreciated!
谢谢!
推荐答案
Windows 资源管理器使用的 API 称为:
Windows Explorer uses an API called:
StrCmpLogicalW
以逻辑"方式执行排序.
to perform the sort in a "logical" manner.
有人也在 C# 中实现了一个类,它会为你做这件事.
Someone has also implemented a class in C# which will do this for you.
这篇关于如何按名称对文件列表进行排序以匹配 Windows 资源管理器显示它们的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!