0

我们正在将服务器环境从一个网络迁移到另一个网络。网络完全分开,彼此看不到。我正在编写一个程序,它将我们当前生产文件服务器上的文件与我们未来生产文件服务器上的文件进行比较。

该程序需要列出以下内容:

  • 当前生产服务器的未来服务器中缺少文件
  • 来自我们生产服务器的未来服务器中过期的文件

我能想到的最快方法是创建一个程序来浏览每个文件夹并创建一个对象来保存所有文件和文件夹。然后我采用该结构并将其序列化为 XML 格式。最终结果是我将有两个文件,其中包含每台服务器上的所有文件和文件夹。

我现在的问题是我需要一种简单的方法来比较这两个文件以查看任何差异。我想到的方法是将当前的生产 XML 文件反序列化回对象,并循环检查每个文件/文件夹以查看文件是否存在于未来的生产服务器上。

除了手动循环遍历每个文件之外,是否有更简单的方法来比较两个 XML 文件以查看哪些对象不同?

这是我用来生成这两个文件的代码:

Imports System.IO

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim objFolder As New Folder
        objFolder = GetFolder("FOLDER TO BROWSE")

        Dim strObjects As String = SerializeObject(objFolder)
        With New StreamWriter("Out Path")
            .Write(strObjects)
        End With

    End Sub


    Function GetFolder(ByVal strPath As String) As Folder
        Dim objFolder As New Folder
        For Each File In New DirectoryInfo(strPath).GetFiles
            Dim oFile As New File
            oFile.Name = File.Name
            oFile.FullName = File.FullName
            oFile.DateCreated = File.CreationTime
            oFile.DateModified = File.LastWriteTime
            objFolder.lstFiles.Add(oFile)
        Next

        For Each Folder In New DirectoryInfo(strPath).GetDirectories
            objFolder.lstFolders.Add(GetFolder(Folder.FullName))
        Next

        Return objFolder
    End Function

    Public Shared Function SerializeObject(ByVal objToSerialize As Object) As String
        Dim objXML As New Xml.Serialization.XmlSerializer(objToSerialize.GetType) 
        Dim sw As New IO.StringWriter() 
        objXML.Serialize(sw, objToSerialize) 
        Return sw.ToString() 
    End Function

    Public Shared Function DeserializeObject(ByVal strSerializedObject As String, ByVal objType As Type) As Object
        Dim objXML As New Xml.Serialization.XmlSerializer(objType) 
        Dim sr As New IO.StringReader(strSerializedObject)
        Return CType(objXML.Deserialize(sr), Object) 
    End Function

End Class

<Serializable(), Xml.Serialization.XmlInclude(GetType(File))> _
Public Class Folder
    Public lstFiles As New List(Of File)
    Public lstFolders As New List(Of Folder)
End Class

<Serializable()> _
Public Class File
    Public Name As String
    Public FullName As String
    Public DateCreated As String
    Public DateModified As String
End Class
4

1 回答 1

1

我最终通过使用我上面描述的方法解决了这个问题。我只是反序列化了对象并遍历每个文件,检查目标文件是否存在并比较其他数据点。

这是代码:

Imports System.IO 

Public Class Form1 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        Dim objFolder As New Folder 
        Dim strObjects As String = "" 

        With New StreamReader("INPUT FILE PATH") 
            strObjects = .ReadToEnd 
        End With 

        objFolder = DeserializeObject(strObjects, GetType(Folder)) 
        CheckFiles(objFolder) 

    End Sub 

    Sub CheckFiles(ByVal oFolder As Folder) 
        Dim FileName As String = "OUTPUT FILE PATH" 
        Dim strWrite As String = "" 
        For Each oFile In oFolder.lstFiles 
            If System.IO.File.Exists(oFile.FullName) Then 
                Dim fi As New FileInfo(oFile.FullName) 
                If fi.LastWriteTime <> oFile.DateModified Then 
                    strWrite += vbCrLf & oFile.Name & "," & oFile.FullName & ",File Out of Date," & oFile.DateModified 
                End If 
            Else 
                strWrite += vbCrLf & oFile.Name & "," & oFile.FullName & ",File Does Not Exist," & oFile.DateModified 
            End If 
        Next 
        Dim sw As New StreamWriter(FileName, True) 
        sw.Write(strWrite) 
        sw.Close() 
        sw.Dispose() 

        For Each oFolder2 In oFolder.lstFolders 
            CheckFiles(oFolder2) 
        Next 
    End Sub 

    Public Shared Function SerializeObject(ByVal objToSerialize As Object) As String 
        Dim objXML As New Xml.Serialization.XmlSerializer(objToSerialize.GetType) 
        Dim sw As New IO.StringWriter()
        objXML.Serialize(sw, objToSerialize)
        Return sw.ToString() 
    End Function 

    Public Shared Function DeserializeObject(ByVal strSerializedObject As String, ByVal objType As Type) As Object 
        Dim objXML As New Xml.Serialization.XmlSerializer(objType)
        Dim sr As New IO.StringReader(strSerializedObject) 
        Return CType(objXML.Deserialize(sr), Object) 
    End Function 

End Class 

<Serializable(), Xml.Serialization.XmlInclude(GetType(File))> _ 
Public Class Folder 
    Public lstFiles As New List(Of File) 
    Public lstFolders As New List(Of Folder) 
End Class 

<Serializable()> _ 
Public Class File 
    Public Name As String 
    Public FullName As String 
    Public DateCreated As String 
    Public DateModified As String 
End Class
于 2013-09-05T20:30:15.770 回答