Link to home
Start Free TrialLog in
Avatar of pmac38CDS
pmac38CDS

asked on

Get the last modified filename in a sub directory

I have a directory with multiple sub directories under it. Each sub directory has one or more files in it. I want to get the filename of the file that has been last modified. How do I go about it ?

Thanks,
Aditya
ASKER CERTIFIED SOLUTION
Avatar of nishant joshi
nishant joshi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could also sort the list.  Something like:
string path = @"c:\some\path";
            List<string> files = new List<string>();
            files.AddRange(System.IO.Directory.GetFiles(path, "*", System.IO.SearchOption.AllDirectories));
            files.Sort(delegate(string file1, string file2)
            {
                return System.IO.File.GetLastWriteTime(file2).CompareTo(System.IO.File.GetLastWriteTime(file1));
            });
            System.IO.FileInfo fi = new System.IO.FileInfo(files[0]);
            MessageBox.Show(fi.LastWriteTime.ToString() + Environment.NewLine + fi.FullName);

Open in new window

is there any need of sort?????
Just depends on what you're doing...

What if later they want the five most recently modified files?

...or they want to sort on multiple parameters?

Sorting gives more flexibility.
ok. great and what about performance for to get 1 file that was last modified?
Like I said, it depends on what you're doing.  I was just presenting another valid approach.  It's up to the author to decide what is best for his or her situation.  =)