NEWS

[VB.NET] Kiểm tra hai file text có trùng dữ liệu hay không (Check Difference two file)

[VB.NET] Kiểm tra hai file text có trùng dữ liệu hay không (Check Difference two file)
Đăng bởi: Thảo Meo - Lượt xem: 10703 14:42:07, 01/04/2017VB.NET   In bài viết

Hôm nay, mình xin hướng dẫn các bạn cách so sánh dữ liệu hai file text có giống nội dung với nhau hay không bằng ngôn ngữ lập trình VB.NET.

Tức là, chương trình sẽ kiểm tra dữ liệu 2 file text. 

Chương trình mình viết hỗ trợ realtime, nghĩa là khi một trong 2 file dữ liệu thay đổi, thì chương trình của chúng ta sẽ tự động đi kiểm tra.

Nếu 2 file dữ liệu khác nhau, thì chương trình sẽ xuất thông báo cho chúng ta biết dữ liệu bị sai, không trùng nhau.

Chương trình demo:

compare two file text vb.net

Source code bằng VB.NET

Imports System.IO
Imports System.Media
Imports System.Threading

Public Class Form1

    Private Function FileCompare(ByVal file1 As String, ByVal file2 As String) As Boolean
        Dim file1byte As Integer
        Dim file2byte As Integer
        Dim fs1 As FileStream
        Dim fs2 As FileStream
        If (file1 = file2) Then
            Return True
        End If
        Thread.Sleep(200)
        fs1 = New FileStream(file1, FileMode.Open)
        fs2 = New FileStream(file2, FileMode.Open)
        If (fs1.Length <> fs2.Length) Then
            fs1.Close()
            fs2.Close()
            Return False
        End If
        Do
            file1byte = fs1.ReadByte()
            file2byte = fs2.ReadByte()
        Loop While ((file1byte = file2byte) And (file1byte <> -1))
        fs1.Close()
        fs2.Close()

        Return ((file1byte - file2byte) = 0)
    End Function

    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        btnStart.Enabled = False
        btnStop.Enabled = True
        fsw.IncludeSubdirectories = True
        fsw.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.Size

        AddHandler fsw.Changed, AddressOf fsw_changed
        fsw.EnableRaisingEvents = True

    End Sub

    Private Sub fsw_changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)
        'setLabelTxt("Monitoring: " & e.FullPath, Label2)
        Dim result As String
        Dim thoigian As String
        thoigian = System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")
        If (FileCompare(txtFile1.Text, txtFile2.Text)) Then
            result = thoigian + " - Tình trạng: OK" + vbNewLine + rtfResult.Text
            setLabelTxt(result, rtfResult)
        Else
            result = thoigian + " - Tình trạng: FAIL" + vbNewLine + rtfResult.Text
            setLabelTxt(result, rtfResult)
            SystemSounds.Beep.Play()
        End If
    End Sub
    Public Shared Sub setLabelTxt(ByVal text As String, ByVal lbl As RichTextBox)
        If lbl.InvokeRequired Then
            lbl.Invoke(New setLabelTxtInvoker(AddressOf setLabelTxt), text, lbl)
        Else
            lbl.Text = text
        End If
    End Sub
    Public Delegate Sub setLabelTxtInvoker(ByVal text As String, ByVal lbl As RichTextBox)

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If opendialogResult.ShowDialog = DialogResult.OK Then
            Dim path As String = opendialogResult.FileName
            txtFile1.Text = path
        End If
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If opendialogResult.ShowDialog = DialogResult.OK Then
            Dim path As String = opendialogResult.FileName
            txtFile2.Text = path
        End If
    End Sub

    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        btnStart.Enabled = True
        btnStop.Enabled = False
        fsw.EnableRaisingEvents = False

    End Sub
End Class

Download source

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[VB.NET] Kiểm tra hai file text có trùng dữ liệu hay không (Check Difference two file)
Đăng bởi: Thảo Meo - Lượt xem: 10703 14:42:07, 01/04/2017VB.NET   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp