2011-09-30 15 views
1

如何用字母数字排序下面这个目录中的文件? 一个文件的例子:12325_2011.jpgVB.NET用字母数字排序目录中的文件

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    If Not Page.IsPostBack Then 
     Dim di As New IO.DirectoryInfo(ImagePath) 
     Dim imageArray As IO.FileInfo() = di.GetFiles() 
     Dim image As IO.FileInfo 

     'list the names of all images in the specified directory 

     For Each image In imageArray 
      CheckBoxList1.Items.Add(image.Name) 
     Next 
    End If 
End Sub 

回答

4

只需修改现有的象这样的每循环:

For Each image In imageArray.OrderBy(Function(i) i.Name) 
    CheckBoxList1.Items.Add(image.Name) 
Next 
+0

你能不能解释一下什么功能(I)i.Name是什么? – Bruno

+0

这是一个lambda表达式 - 一个没有名称的小型迷你函数,它告诉OrderBy函数如何比较数组中的项目进行排序。 –

+0

这适用于'creationtime'或任何其他属性.. – Markive