List Last Accessed Files
Retrieves files which are last accessed.
The following code snippet will return the list of files that are last accessed in the given directory(i.e D:\Shekar\MyFolder). If the given directory does not exists it will return null.
The method will return the IOrderedEnumerable. Here IOrderedEnumerable is an interface which represents a sorted sequence from Linq. This provides other extension methods which can be operated on the sorted list.
For more information on the interface
http://msdn.microsoft.com/en-us/library/bb534852.aspx
IOrderedEnumerable<KeyValuePair<string, DateTime>> list = GetFiles(@"D:\Shekar\MyFolder");
private static IOrderedEnumerable<KeyValuePair<string, DateTime>> GetFiles(string directoryPath)
{
if (Directory.Exists(directoryPath))
{
string[] files = Directory.GetFiles(directoryPath);
Dictionary<string, DateTime> filePathsWithLastAcces = new Dictionary<string, DateTime>();
foreach (string filePath in files)
{
filePathsWithLastAcces.Add(filePath, Directory.GetLastWriteTime(filePath));
}
IOrderedEnumerable<KeyValuePair<string, DateTime>> enumsList = filePathsWithLastAcces.OrderByDescending(fp => fp.Value);
return enumsList;
}
else
{
return null;
}
}