468,768 Members | 1,690 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,768 developers. It's quick & easy.

Listing Directories by Creation Date

I know that you can list a directories and subdirectories using the
DirectoryInfo Class. However, is it possible to sort the directories by
creation date?

Thank you!
Nov 19 '05 #1
3 1326
you load them into an ArrayList or Array and sort.

-- bruce (sqlwork.com)

"RHPT" <RH**@online.nospam> wrote in message
news:44**********************************@microsof t.com...
I know that you can list a directories and subdirectories using the
DirectoryInfo Class. However, is it possible to sort the directories by
creation date?

Thank you!

Nov 19 '05 #2
Hi,

Yes you can. Create your own class that implements IComparer interface and
use it when calling Array.Sort method. Please fing a code snippet below.

public class DirectoryInfoComparer : System.Collections.IComparer
{
public DirectoryInfoComparer()
{
}

public int Compare(object o1, object o2)
{
System.IO.DirectoryInfo info1 =
(System.IO.DirectoryInfo) o1;
System.IO.DirectoryInfo info2 =
(System.IO.DirectoryInfo) o2;

return DateTime.Compare(
info1.CreationTime,
info2.CreationTime);
}
}
// and test
private void button1_Click(object sender, System.EventArgs e)
{
DirectoryInfoComparer comparer =
new DirectoryInfoComparer();

System.IO.DirectoryInfo info =
new System.IO.DirectoryInfo("c:\\");
System.IO.DirectoryInfo[] infos =
info.GetDirectories();

Array.Sort(infos, comparer);

foreach (System.IO.DirectoryInfo i in infos)
{
listBox1.Items.Add(i.FullName + " : " + i.CreationTime);
}
}
Hope this helps
--
Milosz Skalecki
MCP, MCAD
"RHPT" wrote:
I know that you can list a directories and subdirectories using the
DirectoryInfo Class. However, is it possible to sort the directories by
creation date?

Thank you!

Nov 19 '05 #3
Thank you!!

"Milosz Skalecki" wrote:
Hi,

Yes you can. Create your own class that implements IComparer interface and
use it when calling Array.Sort method. Please fing a code snippet below.

public class DirectoryInfoComparer : System.Collections.IComparer
{
public DirectoryInfoComparer()
{
}

public int Compare(object o1, object o2)
{
System.IO.DirectoryInfo info1 =
(System.IO.DirectoryInfo) o1;
System.IO.DirectoryInfo info2 =
(System.IO.DirectoryInfo) o2;

return DateTime.Compare(
info1.CreationTime,
info2.CreationTime);
}
}
// and test
private void button1_Click(object sender, System.EventArgs e)
{
DirectoryInfoComparer comparer =
new DirectoryInfoComparer();

System.IO.DirectoryInfo info =
new System.IO.DirectoryInfo("c:\\");
System.IO.DirectoryInfo[] infos =
info.GetDirectories();

Array.Sort(infos, comparer);

foreach (System.IO.DirectoryInfo i in infos)
{
listBox1.Items.Add(i.FullName + " : " + i.CreationTime);
}
}
Hope this helps
--
Milosz Skalecki
MCP, MCAD
"RHPT" wrote:
I know that you can list a directories and subdirectories using the
DirectoryInfo Class. However, is it possible to sort the directories by
creation date?

Thank you!

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by SU News Server | last post: by
3 posts views Thread by Steven Blair | last post: by
3 posts views Thread by Nathan Young | last post: by
2 posts views Thread by Kurt Schroeder | last post: by
8 posts views Thread by gil | last post: by
2 posts views Thread by ngr | last post: by
4 posts views Thread by techusky | last post: by
3 posts views Thread by kimiraikkonen | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.