Home All Groups Group Topic Archive Search About

Sort Directory.GetCurrentDirectory()

Author
14 Jun 2009 3:46 PM
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

Author
14 Jun 2009 3:54 PM
Herfried K. Wagner [MVP]
"BobAchgill" <BobAchg***@discussions.microsoft.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
14 Jun 2009 3:56 PM
Family Tree Mike
"BobAchgill" <BobAchg***@discussions.microsoft.com> wrote in message
news:017138AD-FE14-4779-8DFC-9DD3945BC0CC@microsoft.com...
> 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.

--
Mike
Author
14 Jun 2009 6:09 PM
BobAchgill
Thanks Herfried and Mike!