File Directory Query functionality

  • Thread starter Thread starter andyblum
  • Start date Start date
A

andyblum

Anyone ever see any code to get a list of files from a directory by
applying another filter instead of name. We can filter by name using
DirectoryInfo.GetFiles, but what about querying by LastAccessedDate and
other file attributes? Is their an easy way to sort an array of
classes such as FileInfo by an attribute inside the class?
 
In .NET 2.0, you can take your array of DirectoryInfo classes and then
write this:

// The directory info instances.
DirectoryInfo[] infos = ...

// Sort the array in place:
Array.Sort(infos,
delegate(DirectoryInfo x, DirectoryInfo y)
{
// Compare the two dates. If they are equal, then return 0. If the
time is
// greater on x, return 1, else, return -1.
if (x.LastAccessedDate > y.LastAccessedDate)
{
// Return 1.
return 1;
}

// If x < y, return -1.
if (x.LastAccessedDate < y.LastAccessedDate)
{
// Return -1.
return -1;
}

// They are the same, return 0.
return 0;
});

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top