I have a list of directories in a parent directory. These directories will be created in a format like 00001, 00002, 00003... so that the one with the bigger trailing number is the recent one. in the above instance, it is 00003. I want to get this programmatically.
thanks for any help..
Try this:
var first = Directory.GetDirectories(@"C:\")
   .OrderByDescending(x => x).FirstOrDefault();
Obviously replace C:\ with the path of the parent directory.
.NET 2:
    private void button1_Click(object sender, EventArgs e) {
        DirectoryInfo di = new DirectoryInfo(@"C:\Windows");
        DirectoryInfo[] dirs = di.GetDirectories("*", 
            SearchOption.TopDirectoryOnly);
        Array.Sort<DirectoryInfo>(dirs, 
            new Comparison<DirectoryInfo>(CompareDirs);
    }
    int CompareDirs(DirectoryInfo a, DirectoryInfo b) {
        return a.CreationTime.CompareTo(b.CreationTime);
    }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With