.net 排序 Directory.GetFiles()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting Directory.GetFiles()
提问by Joel Coehoorn
System.IO.Directory.GetFiles()returns a string[]. What is the default sort order for the returned values? I'm assuming by name, but if so how much does the current culture effect it? Can you change it to something like creation date?
System.IO.Directory.GetFiles()返回一个string[]. 返回值的默认排序顺序是什么?我是按名字假设的,但如果是这样,当前的文化对它有多大影响?您可以将其更改为创建日期之类的内容吗?
Update:MSDN points out that the sort order is not guaranteed for .Net 3.5, but the 2.0 version of the page doesn't say anything at all and neither page will help you sort by things like creation or modification time. That information is lost once you have the array (it contains only strings). I could build a comparer that would check for each file it gets, but that means accessing the file system repeatedly when presumably the .GetFiles() method already does this. Seems very inefficient.
更新:MSDN 指出 .Net 3.5 不能保证排序顺序,但该页面的 2.0 版本根本没有说明任何内容,而且这两个页面都不会帮助您按创建或修改时间等内容进行排序。一旦您拥有数组(它只包含字符串),该信息就会丢失。我可以构建一个比较器来检查它获取的每个文件,但这意味着在 .GetFiles() 方法可能已经这样做时重复访问文件系统。似乎非常低效。
回答by Ian Nelson
If you're interested in properties of the files such as CreationTime, then it would make more sense to use System.IO.DirectoryInfo.GetFileSystemInfos(). You can then sort these using one of the extension methods in System.Linq, e.g.:
如果您对 CreationTime 等文件的属性感兴趣,那么使用 System.IO.DirectoryInfo.GetFileSystemInfos() 会更有意义。然后,您可以使用 System.Linq 中的一种扩展方法对它们进行排序,例如:
DirectoryInfo di = new DirectoryInfo("C:\");
FileSystemInfo[] files = di.GetFileSystemInfos();
var orderedFiles = files.OrderBy(f => f.CreationTime);
Edit - sorry, I didn't notice the .NET2.0 tag so ignore the LINQ sorting. The suggestion to use System.IO.DirectoryInfo.GetFileSystemInfos() still holds though.
编辑 - 抱歉,我没有注意到 .NET2.0 标签,所以忽略 LINQ 排序。使用 System.IO.DirectoryInfo.GetFileSystemInfos() 的建议仍然成立。
回答by Chris Karcher
In .NET 2.0, you'll need to use Array.Sort to sort the FileSystemInfos.
在 .NET 2.0 中,您需要使用 Array.Sort 对 FileSystemInfos 进行排序。
Additionally, you can use a Comparer delegate to avoid having to declare a class just for the comparison:
此外,您可以使用比较器委托来避免仅为比较而声明一个类:
DirectoryInfo dir = new DirectoryInfo(path);
FileSystemInfo[] files = dir.GetFileSystemInfos();
// sort them by creation time
Array.Sort<FileSystemInfo>(files, delegate(FileSystemInfo a, FileSystemInfo b)
{
return a.LastWriteTime.CompareTo(b.LastWriteTime);
});
回答by sebastiaan
Here's the VB.Net solution that I've used.
这是我使用过的 VB.Net 解决方案。
First make a class to compare dates:
首先创建一个类来比较日期:
Private Class DateComparer
Implements System.Collections.IComparer
Public Function Compare(ByVal info1 As Object, ByVal info2 As Object) As Integer Implements System.Collections.IComparer.Compare
Dim FileInfo1 As System.IO.FileInfo = DirectCast(info1, System.IO.FileInfo)
Dim FileInfo2 As System.IO.FileInfo = DirectCast(info2, System.IO.FileInfo)
Dim Date1 As DateTime = FileInfo1.CreationTime
Dim Date2 As DateTime = FileInfo2.CreationTime
If Date1 > Date2 Then Return 1
If Date1 < Date2 Then Return -1
Return 0
End Function
End Class
Then use the comparer while sorting the array:
然后在对数组进行排序时使用比较器:
Dim DirectoryInfo As New System.IO.DirectoryInfo("C:\")
Dim Files() As System.IO.FileInfo = DirectoryInfo.GetFiles()
Dim comparer As IComparer = New DateComparer()
Array.Sort(Files, comparer)
回答by Kibbee
Dim Files() As String
Files = System.IO.Directory.GetFiles("C:\")
Array.Sort(Files)
回答by Guy Starbuck
From msdn:
从msdn:
The order of the returned file names is not guaranteed; use the Sort() method if a specific sort order is required.
不保证返回文件名的顺序;如果需要特定的排序顺序,请使用 Sort() 方法。
The Sort()method is the standard Array.Sort(), which takes in IComparables (among other overloads), so if you sort by creation date, it will handle localization based on the machine settings.
该排序()方法是标准的Array.Sort(),它在IComparables(其他重载之间),因此,如果您按创建日期,它会根据机器设置的处理本地化。
回答by GateKiller
You are correct, the default is my name asc. The only way I have found to change the sort order it to create a datatable from the FileInfo collection.
你是对的,默认是我的名字 asc。我发现更改排序顺序以从 FileInfo 集合创建数据表的唯一方法。
You can then used the DefaultView from the datatable and sort the directory with the .Sort method.
然后,您可以使用数据表中的 DefaultView 并使用 .Sort 方法对目录进行排序。
This is quite involve and fairly slow but I'm hoping someone will post a better solution.
这相当复杂而且相当缓慢,但我希望有人会发布更好的解决方案。
回答by Vertigo
You can implement custom iComparer to do sorting. Read the file info for files and compare as you like.
您可以实现自定义 iComparer 来进行排序。阅读文件的文件信息并根据需要进行比较。
IComparer comparer = new YourCustomComparer();
Array.Sort(System.IO.Directory.GetFiles(), comparer);
回答by skeltech
A more succinct VB.Net version...is very nice. Thank you. To traverse the list in reverse order, add the reverse method...
一个更简洁的 VB.Net 版本......非常好。谢谢你。要以相反的顺序遍历列表,请添加 reverse 方法...
For Each fi As IO.FileInfo In filePaths.reverse
' Do whatever you wish here
Next
回答by Tina Marie
You could write a custom IComparer interface to sort by creation date, and then pass it to Array.Sort. You probably also want to look at StrCmpLogical, which is what is used to do the sorting Explorer uses (sorting numbers correctly with text).
您可以编写自定义 IComparer 接口以按创建日期排序,然后将其传递给 Array.Sort。您可能还想查看 StrCmpLogical,它用于进行 Explorer 使用的排序(使用文本对数字进行正确排序)。
回答by samjudson
If you want to sort by something like creation date you probably need to use DirectoryInfo.GetFiles and then sort the resulting array using a suitable Predicate.
如果您想按创建日期之类的内容排序,您可能需要使用 DirectoryInfo.GetFiles,然后使用合适的 Predicate 对结果数组进行排序。

