根据修改日期比较两个文件夹的文件

时间:2013-05-15 17:43:31

标签: vb.net

我有两个文件夹,说A和B完全相同,没有.of文件同名,但修改日期和大小不同。我想将A的每个文件与其文件夹B的相应文件进行比较,并在列表框中获取新修改的文​​件。如果新修改的文​​件属于文件夹A,则它将进入ListBox1,如果它属于文件夹B,则进入listbox2。 我已经启动了代码但是m敲了.. !!你能帮助我吗?

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)

现在该做什么???

1 个答案:

答案 0 :(得分: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