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

时间:2017-01-12 00:04:29

标签: vb.net binaryfiles

我有一个工作程序,但它就像弗兰肯斯坦 - 其他程序的一部分放在一起,这可能是多余的。这是我正在尝试做的事情: 在二进制文件中找到一个字符串&从该位置到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

1 个答案:

答案 0 :(得分:-1)

在寻找性能时,最好避免尝试逐字节地完成这种事情。相反,你应该使用.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 Air\snowboarding.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

RegEx模式定义如下:

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.