locked
directoryinfo sorting by filename RRS feed

  • Question

  • User2045693258 posted

    I'm looping trhough some files in a folder, is there any way to arrange them by filename alphabetically?

    here is what i have so far

    Dim diFiles As DirectoryInfo = New DirectoryInfo(Server.MapPath(mypath))
    Dim aryFi As IO.FileInfo() = diFiles.GetFiles("*")
    Dim fi As IO.FileInfo
    For Each fi In aryFi

    Next

    Monday, February 23, 2009 11:02 AM

Answers

  • User-1360095595 posted

    One idea I thought of is copying the results into a List<string> (possibly using AddRange) and using the list's Sort method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 23, 2009 2:17 PM

All replies

  • User-1360095595 posted

    Does this work:

    For Each fi In aryFi.OrderBy(Function(s) s.FullName) 
    Next 
     
    Monday, February 23, 2009 11:08 AM
  • User2045693258 posted

     no, is that some c# you've converted?

     

    orderby isn't a member of system.array

    Monday, February 23, 2009 11:20 AM
  • User-1360095595 posted

    It's an extension method with a lambda expression.  Are you using .net Framework 3.5?

    Monday, February 23, 2009 11:34 AM
  • User2045693258 posted

     no, good old 2.0 for the moment (damn my lack of dedicated server hosting!)

    Monday, February 23, 2009 11:42 AM
  • User-1360095595 posted

    One idea I thought of is copying the results into a List<string> (possibly using AddRange) and using the list's Sort method.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 23, 2009 2:17 PM
  • User-1207652161 posted

    Dude here is your code

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections;
    using System.IO;
    
    /// <summary>
    /// Summary description for CompareFiles
    /// </summary>
    public class CompareFiles:IComparer
    {
    	public CompareFiles()
    	{
    		
    	}
    
        #region IComparer Members
    
        public int Compare(object x, object y)
        {
            int result = 0;
            if((x is FileInfo) && (y is FileInfo))
            {
                FileInfo X = (FileInfo)x;
                FileInfo Y = (FileInfo)y;
    
                result = X.Name.CompareTo(Y.Name);
            }
            return result;
        }
    
        #endregion
    }


    // In your Asp.net Page

           CompareFiles compareFiles = new CompareFiles();
            
            DirectoryInfo D = new DirectoryInfo(@"D:\\SHAREDLOCATIONIMAGES");
            FileInfo[] filesInfo =  D.GetFiles();
            ArrayList arl = new ArrayList(filesInfo);
            arl.Sort(compareFiles);

    Thursday, December 17, 2009 9:00 AM