- [C#] Hướng dẫn giải nén file *.rar với tiến trình progress bar winform
- [C#] Chia sẻ source code make Crazy Error Message Effect Bomb Windows
- [C#] Lập trình ứng dụng theo mô hình MVP Model-View-Presenter Pattern Winform
- [C#] Giới thiệu và những thứ hay ho từ thư viện System.Reactive của Microsoft
- [C#] Hướng dẫn tạo ứng dụng Chat với GPT sử dụng Open AI API
- [DEVEXPRESS] Tạo month picker trên DateEdit Winform C#
- [DATABASE] Cách sử dụng và lưu ý khi sử dụng khóa ngoại (Foreign Key) trong Sqlserver
- [C#] Garbage Collector (GC) là gì? Cách giải phóng bộ nhớ trên ứng dụng Winform khi các đối tượng không còn sử dụng
- [C#] Cách tính độ tương phản màu sắc Contrast Color mà con người có thể nhìn thấy được
- [C#] Hướng dẫn mã hóa mật khẩu tài khoản ứng dụng đúng chuẩn Men
- [C#] Sử dụng Open AI Chat GPT viết ứng dụng Count down timer có hiệu ứng trên winform
- [DATABASE] Chia sẻ dữ liệu Pantone Color sql và json api
- [SQLSERVER] Tạo mã sản phẩm tự động như: SP0001, SP0002, SP0003... sử dụng Trigger
- [C#] Hướng dẫn kiểm tra phiên bản NET Framework cài đặt ở máy tính
- [C#] Hướng dẫn đọc file excel đơn giản sử dụng thư viện Epplus
- [C#] ConcurrentBag là gì và cách sử dụng nó trong lập trình bất đồng bộ
- [C#] AutoResetEvent là gì và cách sử dụng
- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
[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