none
檔案比對相關問題 RRS feed

  • 問題

  •     請問:

    1.要取消非同步執行,如以下程式碼還有更好的方法嗎?因為我是寫在迴圈裡面,好像沒辦法一押就停止。

    2.怎麼做到能有複製進度表,如列出共有幾個異動檔案,然後有整體進度、單一進度‧

    3.不用api刪檔,底下程式碼遇到唯讀檔該如何修改?

     

    歡迎有興趣寫檔案比對的朋友幫忙修改,thx!

    DoWork1-單機比對主機,單機有多餘資料夾、檔案就砍。

    DoWork2-主機比對單機,檔案大小、最後修改日期不同就複製到單機。

     

      dim SourcePath As String = "d:\game\魔獸爭霸3" '來源路徑
        dim DestinationPath As String = "c:\game\魔獸爭霸3" '目的路徑

    Private Sub frmCompare_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            BackgroundWorker1.WorkerSupportsCancellation = True
            BackgroundWorker2.WorkerSupportsCancellation = True
            BackgroundWorker1.RunWorkerAsync()
            BackgroundWorker2.RunWorkerAsync()
        End Sub

     

        Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            '---------------------------------------
            '比對DestinationPath資料夾,有多餘即刪除
            '---------------------------------------
            Try
                Dim DestinationDdirectory As New DirectoryInfo(DestinationPath)
                Dim DestinationD As DirectoryInfo()

                '取的DestinationPath下所有子資料夾
                DestinationD = DestinationDdirectory.GetDirectories("*", SearchOption.AllDirectories)
                For Each subD As DirectoryInfo In DestinationD
                    If BackgroundWorker1.CancellationPending Then
                        Exit Sub
                    End If
                    Dim ReplaceD As String = Replace(subD.FullName, DestinationPath, SourcePath)
                    '假如SourcePath不存在資料夾路徑,即刪除DestinationPath資料夾
                    If Not (My.Computer.FileSystem.DirectoryExists(ReplaceD)) Then
                        My.Computer.FileSystem.DeleteDirectory(subD.FullName, FileIO.DeleteDirectoryOption.DeleteAllContents)
                    End If
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            '-------------------------------------
            '比對DestinationPath檔案,有多餘即刪除
            '-------------------------------------
            Try
                Dim Destinationfile As New DirectoryInfo(DestinationPath)
                Dim Destinationf As FileInfo()

                '取的DestinationPath下所有檔案
                Destinationf = Destinationfile.GetFiles("*", SearchOption.AllDirectories)
                For Each f As FileInfo In Destinationf
                    If BackgroundWorker1.CancellationPending Then
                        Exit Sub
                    End If
                    Dim Replacef As String = Replace(f.FullName, DestinationPath, SourcePath)
                    '假如SourcePath不存在檔案路徑,即刪除DestinationPath檔案
                    If Not (My.Computer.FileSystem.FileExists(Replacef)) Then
                        My.Computer.FileSystem.DeleteFile(f.FullName)
                    End If
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub

     

     

        Private Sub BackgroundWorker2_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
            '---------------------------------------------------
            '比對SourcePath檔案,有異動即複製(SourcePath第一層)
            '---------------------------------------------------
            Try
                If My.Computer.FileSystem.DirectoryExists(SourcePath) Then
                    Dim thefile As New DirectoryInfo(SourcePath)
                    Dim Sourcef As FileInfo()
                    Sourcef = thefile.GetFiles()
                    For Each f As FileInfo In Sourcef
                        If BackgroundWorker1.CancellationPending Then
                            Exit Sub
                        End If
                        Dim Replacef As String = Replace(f.FullName, SourcePath, DestinationPath)
                        Dim Destinationf As New FileInfo(Replacef)
                        If My.Computer.FileSystem.FileExists(Replacef) Then
                            If (f.LastWriteTime <> Destinationf.LastWriteTime) Or (f.Length <> Destinationf.Length) Then
                                My.Computer.FileSystem.CopyFile(f.FullName, Destinationf.FullName, True)
                            End If
                        Else
                            My.Computer.FileSystem.CopyFile(f.FullName, Destinationf.FullName, True)
                        End If
                    Next
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

            '-----------------------------------
            '比對SourcePath資料夾,不存在即複製
            '-----------------------------------
            Try
                Dim SourceDirectory As New DirectoryInfo(SourcePath)
                Dim SubD As DirectoryInfo()

                '取的SourcePath下所有子資料夾
                SubD = SourceDirectory.GetDirectories("*", SearchOption.AllDirectories)
                For Each SourceD As DirectoryInfo In SubD
                    If BackgroundWorker1.CancellationPending Then
                        Exit Sub
                    End If
                    Dim ReplaceD As String = Replace(SourceD.FullName, SourcePath, DestinationPath)
                    Dim DestinationD As New DirectoryInfo(ReplaceD)
                    If Not (My.Computer.FileSystem.DirectoryExists(ReplaceD)) Then
                        My.Computer.FileSystem.CopyDirectory(SourceD.FullName, DestinationD.FullName, True)
                    Else

                        '--------------------------------
                        '比對SourcePath檔案,有異動即複製
                        '--------------------------------
                        Dim Sourcef As FileInfo()

                        '取的子資料夾下的所有檔案
                        Sourcef = SourceD.GetFiles("*", SearchOption.AllDirectories)
                        For Each f As FileInfo In Sourcef
                            If BackgroundWorker1.CancellationPending Then
                                Exit Sub
                            End If
                            Dim Replacef As String = Replace(f.FullName, SourcePath, DestinationPath)
                            Dim Destinationf As New FileInfo(Replacef)
                            If My.Computer.FileSystem.FileExists(Replacef) Then
                                If (f.LastWriteTime <> Destinationf.LastWriteTime) Or (f.Length <> Destinationf.Length) Then
                                    My.Computer.FileSystem.CopyFile(f.FullName, Destinationf.FullName, True)
                                End If
                            Else
                                My.Computer.FileSystem.CopyFile(f.FullName, Destinationf.FullName, True)
                            End If
                        Next
                    End If
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub

     

        Private Sub BackgroundWorker2_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
            If Not (BackgroundWorker2.IsBusy) Then
                Form1.Show()
                Me.Close()
            End If
        End Sub
    End Class

    2007年6月14日 上午 10:26

解答

  • 1. 要取消 BackgroundWorker 的工作,就要呼叫 BackgroundWorker.CancelAsync() 方法。

    2. 使用 BackgroundWorker 的 ReportProgress 功能即可實作。

    3. 只有改掉唯讀屬性才可以刪除。

    2007年6月15日 上午 02:42
    版主