473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Retrieving Sorted Directories

Is there an easy way to loop through directories sorted by, say,
CreationTime or LastAccessTime? It seems like a reasonable
capability, but I don't see it.

v/r
bk

Oct 24 '07 #1
2 1720
br****************@gmail.com wrote:
Is there an easy way to loop through directories sorted by, say,
CreationTime or LastAccessTime? It seems like a reasonable
capability, but I don't see it.
Define "easy".

I'm not aware of a single method in .NET that does this. But it should
not be all that hard to use Directory.GetDirectories() to obtain a list
of directories in a given directory, and then sort that list according
to their properties (such as timestamps, as your example).

In the most basic implementation, you would just use an Array.Sort()
overload that takes an IComparer instance, where your IComparer
retrieves the relevant property for each of the two array elements
passed to it. If you're comfortable with generics, you might prefer the
overload that uses a Comparison<Tdelegate.

If you want to only retrieve the property information once for each
directory, you could create a new list of DirectoryInfo instances and
sort that instead. But I think that unless you are dealing with a huge
number of directories, this might be overkill.

Pete
Oct 24 '07 #2
br****************@gmail.com wrote:
Is there an easy way to loop through directories sorted by, say,
CreationTime or LastAccessTime? It seems like a reasonable
capability, but I don't see it.
Example:

using System;
using System.Collections.Generic;
using System.IO;

public class TestClass
{
public static void Main(string[] args)
{
DirectoryInfo di = new DirectoryInfo(@"C:\");
FileInfo[] fi = di.GetFiles();
for(int i = 0; i < fi.Length; i++)
{
Console.WriteLine(fi[i].Name);
}
Array.Sort(fi, new FileInfoComparer());
for(int i = 0; i < fi.Length; i++)
{
Console.WriteLine(fi[i].Name);
}
Console.ReadKey();
}
}

public class FileInfoComparer : IComparer<FileInfo>
{
public int Compare(FileInfo x, FileInfo y)
{
return x.CreationTime.CompareTo(y.CreationTime);
}
}

Arne
Oct 25 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rushabh Dadbhawala | last post by:
Problem: I am fetching data from the database, storing it in a dataSet, and binding it with a Data Grid. The DataGrid controls allows sorting. The problem is that when the data is sorted on one...
1
by: Silverton Mike | last post by:
I need to retrieve Windows Media Player playlists programmatically via C# from a desktop machine. Does WMP keep track of used/created playlists via registry entries or a saved file on the hard...
2
by: Alan Lambert | last post by:
If have an asp.net page (code behind written in VB.NET) which needs to store information from a postback in an arraylist and retain this list across multiple postbacks. The variable is declared...
5
by: Carol | last post by:
Hi, I am in a problem. I am sending files to a particular folder one after the another. Than obviosly there create time is different. Now suppose if there are 50 files and i want to retrive the...
4
by: Dino Buljubasic | last post by:
Hi, I have noticed that when I use Socket.Receive(byte) it retrieves all information about files including day, month but without year. Date for directories is full though. What would be...
5
by: himilecyclist | last post by:
I have a query screen where the user has an option to search by name fields in the database. There are first, middle and last name fields and the results returned should be sorted last, first,...
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
1
by: xxxx | last post by:
Dear All :) i'm using c# , VS 2003 and IIS 6 i was wondering how can i get all the virtual directory folders available on the local server using c#. i'm trying to build a desktop application...
8
by: WildernessCat | last post by:
Hello there! Suppose I have an associative array that looks like this: $arr = 'some value'; $arr = 'some value'; $arr = 'some value'; $arr = 'some value'; $arr = 'some value'; $arr = 'some...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.