学习VB.NET时,你可能会遇到VB.NET文件排序的问题,这里将介绍VB.Net按文件尺寸大小排序的实例,在这里拿出来和大家分享一下。
VB.NET文件排序之按文件尺寸大小代码如下:
- Public Class Form1
- '按照文件尺寸大小排序
- Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
- Dim MyDir As String = Me.textBox1.Text
- Dim MyFiles = GetFiles(MyDir)
- Dim MyQuery = From MyFile In MyFiles _
- Order By (GetLength(MyFile)) _
- Select MyFile
- Me.dataGridView1.DataSource = MyQuery.ToArray()
- End Sub
- Function GetFiles(ByVal MyDir As String) As IEnumerable(Of System.IO.FileInfo)
- If (Not System.IO.Directory.Exists(MyDir)) Then
- Throw New System.IO.DirectoryNotFoundException()
- End If
- Dim MyFileNames As String()
- Dim MyFiles As List(Of System.IO.FileInfo) = New List(Of System.IO.FileInfo)()
- '查找指定目录下的所有子目录中的所有文件
- 'MyFileNames = System.IO.Directory.GetFiles(MyDir, "*.*", _
- 'System.IO.SearchOption.AllDirectories)
- MyFileNames = System.IO.Directory.GetFiles(MyDir)
- For Each MyName As String In MyFileNames
- MyFiles.Add(New System.IO.FileInfo(MyName))
- Next
- Return MyFiles
- End Function
- Function GetLength(ByVal MyFileInfo As System.IO.FileInfo) As Long
- Dim MyLength As Long = 0
- Try '更多.net源码和实例
- MyLength = MyFileInfo.Length
- Catch ex As System.IO.FileNotFoundException
- MyLength = 0
- End Try
- Return MyLength
- End Function
- End Class
以上就是VB.NET文件排序的代码,大家学会了吗?
【编辑推荐】