Sort Directory.GetCurrentDirectory()

  • Thread starter Thread starter BobAchgill
  • Start date Start date
B

BobAchgill

How can I sort the contents of Directory.GetCurrentDirectory() by file create
data?

If that is not possible how can I sort it by file name?

Thanks!

Bob
 
BobAchgill said:
How can I sort the contents of Directory.GetCurrentDirectory() by file
create
data?

If that is not possible how can I sort it by file name?

\\\
Public Class LastWriteTimeComparer
Implements IComparer

Public Function Compare( _
ByVal x As Object, _
ByVal y As Object _
) As Integer Implements IComparer.Compare
Return _
Math.Sign( _
File.GetLastWriteTime(DirectCast(x, String)).Ticks - _
File.GetLastWriteTime(DirectCast(y, String)).Ticks _
)
End Function
End Class
///

Usage:

\\\
Dim FileNames() As String = Directory.GetFiles("C:\WINDOWS")
Array.Sort(FileNames, New LastWriteTimeComparer())
Me.ListBox1.DataSource = FileNames
///

Alternatively you may want to take a closer look at LINQ, which provides an
ability to order the items.
 
BobAchgill said:
How can I sort the contents of Directory.GetCurrentDirectory() by file
create
data?

If that is not possible how can I sort it by file name?

Thanks!

Bob


Directory.GetCurrentDirectory() returns a single string which is the name of
the current directory. Perhaps you should look at DirectoryInfo.GetFiles()
or Directory.GetFiles(). DirectoryInfo.GetFiles() would give you the access
to the CreateDate information to do the sort.
 
Back
Top