//比较2个文件是否一致
Private Function CompareFiles(ByVal file1 As String, ByVal file2 As String) As Boolean
' Set to true if the files are equal; false otherwise
Dim filesAreEqual As Boolean = False
With My.Computer.FileSystem
' Ensure that the files are the same length before comparing them line by line.
If .GetFileInfo(file1).Length = .GetFileInfo(file2).Length Then
Using file1Reader As New FileStream(file1, FileMode.Open), _
file2Reader As New FileStream(file2, FileMode.Open)
Dim byte1 As Integer = file1Reader.ReadByte()
Dim byte2 As Integer = file2Reader.ReadByte()
' If byte1 or byte2 is a negative value, we have reached the end of the file.
While byte1 >= 0 AndAlso byte2 >= 0
If (byte1 <> byte2) Then
filesAreEqual = False
Exit While
Else
filesAreEqual = True
End If
' Read the next byte.
byte1 = file1Reader.ReadByte()
byte2 = file2Reader.ReadByte()
End While
End Using
End If
End With
Return filesAreEqual
End Function
//读取大文本
Public Function ReadFile(ByVal FilePath As String) As String
Dim Value As String = Nothing
Dim fs As FileStream = File.OpenRead(FilePath)
Dim lineString As String
Using sr As New StreamReader(fs, System.Text.Encoding.Default)
'While Not sr.EndOfStream '循环直到流结束
Static i As Integer '读取行数
Do
lineString = sr.ReadLine() '读一行
If lineString.StartsWith("itm_newa,geo\time") Then
Value += (lineString & vbCrLf)
'Console.WriteLine("Drive:" & lineString.Split("\")(0).ToString())
i += 1
If i >= 20 Then
Exit Do
End If
End If
Value += (lineString & vbCrLf)
Loop Until (sr.EndOfStream)
sr.Close()
End Using
'大文本分字节大小读取
'Using fs As FileStream = File.OpenRead(FilePath)
' Dim b As Byte() = New Byte(1023) {}
' Dim temp As New UTF8Encoding(True)
' While fs.Read(b, 0, b.Length) > 0
' Value += temp.GetString(b)
' End While
'End Using
Return Value
End Function
//递归搜索文件(高效)
Imports System.IO
Public Class Form1
'窗体控件分别是 FolderBrowserDialog1,Button1,Label1,Listbox1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Me.FolderBrowserDialog1.ShowDialog()
Dim Myarr As New ArrayList
Sel(Directory.GetFiles(FolderBrowserDialog1.SelectedPath, "*", SearchOption.AllDirectories), "*.txt", Myarr)
Dim Mystr As String
For Each Mystr In Myarr
ListBox1.Items.Add(Mystr)
Debug.Print(Mystr) '请查看下方即时窗口消息
Next
'Label1.Text = FolderBrowserDialog1.SelectedPath.ToString & "路径下共有文件" _
'& ListBox1.Items.Count.ToString & "个"
End Sub
Private Sub Sel(ByVal Filenames() As String, ByVal FileClass As String, ByRef Myarr As ArrayList) '传入值Filenames() [文件组()地址],FileClass文件类形,地址变量数组列表用来传出文件名
Try
Dim i As Integer
For Each Filenames(i) In Filenames
'Application.DoEvents()
If Filenames(i) Like FileClass Then '文件名匹配筛选
Myarr.Add(Filenames(i))
Dim fi As New System.IO.FileInfo(Filenames(i))
'文件大小控制
'If fi.Length > 1000000000 Then
' MsgBox(filenames(i) & "大于1G")
'End If
End If
Next
Catch ex As Exception
ex.Message.ToString()
End Try
End Sub
End Class