0

我有两个文件夹,说 A 和 B 具有完全相同的 .of 文件,但名称相同但修改日期和大小不同。我想将 A 的每个文件与其文件夹 B 的相应文件进行比较,并在列表框中获取新修改的文​​件。如果新修改的文​​件属于文件夹A,则进入ListBox1,如果属于文件夹B,则进入listbox2。我已经开始了代码,但我被击中了..!! 你们中的任何人都可以帮助我吗?

Dim PathA As String= "" 'path for folder A
Dim PathB As String= "" 'path for folder B
Dim Dir1 As New System.Io.DirectoryInfo(PathA)
Dim Dir2 As New System.Io.DirectoryInfo(PathB)
Dim List1 = Dir1.GetFiles(*.*,System.IO.SearchOption.AllDirectories)
Dim List2 = Dir2.GetFiles(*.*,System.IO.SearchOption.AllDirectories)
Dim myFileCompare As New fileCompare
Dim areIdentical As Boolean = List1.sequence Equals (List2,myFileCompare)

现在要做什么???

4

1 回答 1

2

需要实现一个 FileInfoComparer 然后调用Intersect可枚举扩展

Sub Main
    Dim PathA As String= "D:\temp" 'path for folder A
    Dim PathB As String= "D:\temp2" 'path for folder B
    Dim Dir1 As New System.Io.DirectoryInfo(PathA)
    Dim Dir2 As New System.Io.DirectoryInfo(PathB)
    Dim List1 = Dir1.GetFiles("*.*")
    Dim List2 = Dir2.GetFiles("*.*")

    Dim List3 = List1.Intersect(List2, new FileComparer)

    For Each info in List3
       Console.WriteLine(info.Name)
    Next

End Sub


Public Class FileComparer
    Implements IEqualityComparer(Of FileInfo)

    Public Function Equals1(ByVal x As FileInfo, ByVal y As FileInfo) As Boolean _ 
                    Implements IEqualityComparer(Of FileInfo).Equals


        If x Is y Then Return True 
        If x Is Nothing OrElse y Is Nothing Then Return False 

        Return (x.Name = y.Name) AndAlso (x.Length = y.Length) AndAlso (x.LastWriteTime = y.LastWriteTime)
    End Function 

    Public Function GetHashCode1(ByVal info As FileInfo) As Integer _
                     Implements IEqualityComparer(Of FileInfo).GetHashCode

        If info Is Nothing Then Return 0
        Return info.Name.GetHashCode()
    End Function 
End Class
于 2013-05-15T18:02:25.807 回答