- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[VB.NET] Kiểm tra hai file text có trùng dữ liệu hay không (Check Difference two file)
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:
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