如何优化我的代码 - vb.net



我有一个工作程序,但它就像弗兰肯斯坦 - 其他程序的一部分放在一起,这可能是多余的。这是我正在尝试做的:在二进制文件中查找一个字符串,然后从该位置到EOF将内容转储到字符串中。

这是我的代码:

    Imports System.IO
Public Class Form1
    Dim b() As Byte = IO.File.ReadAllBytes("C:data.bin")
    Dim encodme As New System.Text.ASCIIEncoding
    Dim SearchString As String = "xyzzy"
    Dim bSearch As Byte() = encodme.GetBytes(SearchString)
    Dim bFound As Boolean = True
    Dim oneByte As Byte
    Dim fileData As New IO.FileStream("C:data.bin", FileMode.Open, FileAccess.Read)
    Dim strMessage As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 0 To b.Length - bSearch.Length - 1
            If b(i) = bSearch(0) Then
                bFound = True
                For j As Integer = 0 To bSearch.Length - 1
                    If b(i + j) <> bSearch(j) Then
                        bFound = False
                        Exit For
                    End If
                Next
                If bFound Then
                    fileData.Seek(i + 5, SeekOrigin.Begin)
                    strMessage = ""
                    For r As Integer = (i + 5) To fileData.Length() - 1
                        oneByte = fileData.ReadByte()
                        strMessage = strMessage + Chr(oneByte)

                    Next r
                    MsgBox(strMessage)
                Else
                    MsgBox("File Doesn't have string")
                    Exit Sub
                End If
            End If
        Next


    End Sub
End Class

在寻找性能时,最好避免尝试逐字节遍历这种事情。相反,您应该使用 .NET 为您提供的功能。此示例使用 RegEx 查找任何文件中字符串的所有匹配项,返回每个匹配项及其后面的所有内容,直到下一个匹配项或 UTF-8 字符串中的文件末尾:

Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim matches = FindStringMatchesInFile("C:Infinite Airsnowboarding.exe", "data")
        For Each m In matches
            ...
        Next
    End Sub

    Private Function FindStringMatchesInFile(filename As String,
                                             searchString As String) As List(Of String)
        Dim output As New List(Of String)
        Dim reader = New StreamReader(filename, Encoding.UTF8)
        Dim re = New Regex(String.Format("{0}(?:(?!{0}).)*", searchString),
                           RegexOptions.Singleline Or RegexOptions.IgnoreCase,
                           Regex.InfiniteMatchTimeout)
        Dim matches = re.Matches(reader.ReadToEnd())
        For Each m As Match In matches
            output.Add(m.ToString())
        Next
        Return output
    End Function
End Class

正则表达式模式定义如下:

Matches the characters {searchString} literally (case insensitive)
Non-capturing group (?:(?!{searchString}).)*
* Quantifier — Matches between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Negative Lookahead (?!{searchString})
Assert that the Regex below does not match
Matches the characters {searchString} literally (case insensitive)
. matches any character
Global pattern flags
g modifier: global. All matches (don't return after first match)
s modifier: single line. Dot matches newline characters
i modifier: case insensitive.

最新更新