从小我们就知道组的概念,我们如何按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 As IEnumerable(Of System.IO.FileInfo) = GetFiles(MyDir)
- Dim MyQueryGroup = From MyFile In MyFiles _
- Group MyFile By MyExt = MyFile.Extension.ToLower() _
- Into MyFileGroup = Group _
- Order By MyExt
- Me.listBox1.Items.Clear()
- For Each MyFileGroup In MyQueryGroup
- Me.listBox1.Items.Add("包含" + MyFileGroup.MyExt + "扩展名的文件如下:")
- For Each MyFileInfo In MyFileGroup.MyFileGroup
- Me.listBox1.Items.Add(MyFileInfo.Name)
- Next
- Me.listBox1.Items.Add("")
- Next
- End Sub '更多.net源码和实例,来自乐博网 www.lob.cn
- 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
- End Class
以上就是VB.NET扩展名分组的实例,大家可以试试。
【编辑推荐】