'######### clsCompareFileInfo ############
Imports System.IO
Public Class clsCompareFileInfo
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim File1 As FileInfo
Dim File2 As FileInfo
File1 = DirectCast(x, FileInfo)
File2 = DirectCast(y, FileInfo)
Compare = DateTime.Compare(File1.LastWriteTime, File2.LastWriteTime)
End Function
End Class
'#######################################
'############### Form1 ##################
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dirinfo As DirectoryInfo
Dim allFiles() As FileInfo
Dim sSelPath As String = "C:\Data\A\"
dirinfo = New DirectoryInfo(sSelPath)
allFiles = dirinfo.GetFiles("*.txt")
Array.Sort(allFiles, New clsCompareFileInfo)
For Each fl As FileInfo In allFiles
Label1.Text = fl.LastWriteTime.ToString()
Label2.Text = fl.Name.ToString()
Next
End Sub
'#######################################
Output ก็จะออกมาเป็น วันเวลาที่ Modify ล่าสุด กับ ชื่อไฟล์ ในโฟล์เดอร์ A เท่านั้น
'######### clsCompareFileInfo ############
Imports System.IO
Public Class clsCompareFileInfo
Implements IComparer
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
Dim File1 As FileInfo
Dim File2 As FileInfo
File1 = DirectCast(x, FileInfo)
File2 = DirectCast(y, FileInfo)
Compare = DateTime.Compare(File1.LastWriteTime, File2.LastWriteTime)
End Function
End Class
'#######################################
'############### Form1 ##################
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dirinfo As DirectoryInfo
Dim allFiles() As FileInfo
Dim sSelPath As String = "C:\Data\A\"
dirinfo = New DirectoryInfo(sSelPath)
'allFiles = dirinfo.GetFiles("*.txt") แก้ไขบรรทัดนี้ครับ
allFiles = dirinfo.GetFiles("*.txt", SearchOption.AllDirectories)
Array.Sort(allFiles, New clsCompareFileInfo)
For Each fl As FileInfo In allFiles
Label1.Text = fl.LastWriteTime.ToString()
Label2.Text = fl.Name.ToString()
Next
End Sub
'#######################################
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
' Specify the directories you want to manipulate.
Dim di As DirectoryInfo = New DirectoryInfo("c:\")
Try
' Get only subdirectories that contain the letter "p."
Dim dirs As FileSystemInfo() = di.GetDirectories("*p*")
Console.WriteLine("Number of directories with a p: {0}", dirs.Length)
Dim diNext As DirectoryInfo
' Count all the files in each subdirectory that contain the letter "e."
For Each diNext In dirs
Console.WriteLine("The number of files and directories " + _
"in {0} with an e is {1}", diNext, _
diNext.GetFileSystemInfos("*e*").Length)
Next
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class