What's Here?
- Members: 306,728
- Replies: 840,911
- Topics: 140,495
- Snippets: 4,465
- Tutorials: 1,165
- Total Online: 2,530
- Members: 128
- Guests: 2,402
|
Functions that return sorted Files And Folders in FileInfo And DirectoryInfo Arrays.
|
Submitted By: dzone41
|
|
|
Rating:
|
|
Views: 149 |
Language: VB.NET
|
|
Last Modified: November 4, 2009 |
Instructions: Imports System.IO
Requires an Path String and an integer. The integer would most likely come from selected menu sort options in User Interface.
The "If intSortBy" can be replaced with Select Case if more sort options are needed.
|
Snippet
Private Sub _GetFiles(ByVal strPath As String, ByVal intSortBy As Integer)
'Get all Directories in the Path
folders = New DirectoryInfo (strPath ). GetDirectories
'These functions use the same array and returns it sorted
'Get All Files in the Path
files = New DirectoryInfo (strPath ). GetFiles()
End Sub
Private Function SortFolders (ByVal folders As DirectoryInfo (), ByVal intSortBy as Integer) As DirectoryInfo ()
Dim intCount As Integer = folders. GetUpperBound(0)
'Must Delcare new array
Dim arrTempNames(intCount) As String
Dim intA As Integer = 0
'Iterate through folders
For Each dir As DirectoryInfo In folders
'Check sort option, this can be replaced with Select Case
If intSortBy = 0 Then
'assigns the chosen sortby element to a string array
arrTempNames(intA) = dir.Name
ElseIf intSortBy = 1 Then
arrTempNames(intA) = dir.LastWriteTime.ToString
End If
Next
End Function
Private Function SortFiles (ByVal files As FileInfo (), ByVal intSortBy As Integer) As FileInfo ()
Dim intCount As Integer = files. GetUpperBound(0)
Dim arrTempNames(intCount) As String
Dim intA As Integer = 0
For Each dir As FileInfo In files
If intSortBy = 0 Then
arrTempNames(intA) = dir.Name
ElseIf intSortBy = 1 Then
arrTempNames(intA) = dir.LastWriteTime.ToString
End If
Next
Array. Sort(arrTempNames, files)
End Function
Copy & Paste
|
|
|
|