locked
Which attribute would be used to sort DirectoryInfo.GetDirectories() result ? RRS feed

  • Question

  • I have a folder "Parent" and many sub folders  within it, the sub folders are named as incremental number one by one (some others app will create them dynamically). I must put file into the folder which has "maximal number" of name.

    I have noticed that DirectoryInfo.GetDirectories() returns a DirectoryInfo array to me that sorted by name. So I can get the "maximal number" of name by the following code:

    string path = @"d:\";
    DirectoryInfo dir = new DirectoryInfo(path);
    DirectoryInfo[] subDirArray = dir.GetDirectories();
    DirectoryInfo maximalDir = subDirArray[subDirArray.Length - 1];
    I'm worrying about whether the behavior is reliable or not? Will the behavior be changed in future .net version?

    I`m the coming days...

    Thursday, March 22, 2012 2:43 AM

Answers

  • Do not rely on the order of results that we return from this method, it's actually defined by the file system and not by us, Windows states the following (emphasis mine) (http://msdn.microsoft.com/en-us/library/windows/desktop/aa364428(v=vs.85).aspx):

    The order in which this function returns the file names is dependent on the file system type. With the NTFS     file system and CDFS file systems, the names are usually returned in alphabetical order. With FAT file systems, the names are usually returned in the order the files were written to the disk, which may or may not be in alphabetical order. However, as stated previously, these behaviors are not guaranteed.

    Only rely on behavior explictly called out in the docs.


    Base Class Library Team (BCL) | My Blog: http://davesbox.com


    Friday, March 23, 2012 8:50 PM

All replies

  • I'm sure Microsoft is not going to change the behavior of any its classes and methods without any valid reasons. So dont worry it wont change. Even for some valid reasons they do change, they always provide an alternate for the existing functionalities.

    Having said that, i dont think the GeDirectories() (http://msdn.microsoft.com/en-us/library/s7xk2b58.aspx) method is going to return the sub-directories in a sorted order. Instead you might want to sort them using the DirectoryInfo properties that you get from this call.

    This post talks more about the sorting support http://stackoverflow.com/questions/52842/sorting-directory-getfiles
    Thursday, March 22, 2012 4:03 AM
  • Do not rely on the order of results that we return from this method, it's actually defined by the file system and not by us, Windows states the following (emphasis mine) (http://msdn.microsoft.com/en-us/library/windows/desktop/aa364428(v=vs.85).aspx):

    The order in which this function returns the file names is dependent on the file system type. With the NTFS     file system and CDFS file systems, the names are usually returned in alphabetical order. With FAT file systems, the names are usually returned in the order the files were written to the disk, which may or may not be in alphabetical order. However, as stated previously, these behaviors are not guaranteed.

    Only rely on behavior explictly called out in the docs.


    Base Class Library Team (BCL) | My Blog: http://davesbox.com


    Friday, March 23, 2012 8:50 PM
  • cant you just do it by creation time???

     var lastFolder = Directory.GetDirectories(".").OrderBy((folder) => new FileInfo(folder).CreationTime).last();
    if not:

    public class FolderComparer : IComparer<string> { class Folder : IComparable<Folder> { private Folder() { Name = string.Empty; Index = int.MinValue; } public Folder(string folder): this() { if (string.IsNullOrEmpty(folder)) return; var match = Regex.Matches(folder, @"[a-zA-Z]+|\d+"); if (match.Count == 0) return; var alp = match[0].Value; if (match.Count == 1) return; var temp = match[1].Value; if(!string.IsNullOrEmpty(temp)) { Index = int.Parse(temp); } } public string Name { get; set; } public int Index { get; set; } public int CompareTo(Folder other) { var result = Name.CompareTo(other.Name); return result == 0 ? Index.CompareTo(other.Index) : result; } } public int Compare(string left, string right) { return new Folder(left).CompareTo(new Folder(right)); } }

     

    usage:

    var lastFolder = Directory.GetDirectories(".").OrderBy(folder => folder, new FolderComparer()).Last();



    gimme some slamming techno!!!!


    Sunday, March 25, 2012 8:25 AM
  • Thank you ,I have implement additional logic for the issue.

    I`m the coming days...

    Monday, March 26, 2012 3:34 PM