简体   繁体   中英

How do I optimize my code - vb.net

I have a working program, but it's like Frankenstein - parts of other programs put together, that may be redundant. Here's what I'm trying to do: Find a string inside a binary file & from that location to the EOF dump the contents into a string.

Here is my code:

    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

When looking for performance, it is best to avoid trying to walk through this kind of thing byte by byte. Instead you should use the facilities .NET provides you with. This example uses RegEx to find all matches of a string in any file, returning each match with everything that follows it until the next match or the end of the file in a UTF-8 string:

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

The RegEx pattern definition is the following:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM