在文件中查找特定值

时间:2016-10-20 16:34:31

标签: vb.net

我正在尝试创建一个简单的程序来查找文件中的某些值。这些文件是.arkprofile个文件,属于游戏ARK

如果您在常规编辑器中打开它们,这些.arkprofile文件包含一些可读文本以及一些加扰文本。我需要的所有值都是ASCII,所以我可以手动获取它们,但是这个程序的目的是通过大量文件来获取字符名称。以下是常规文本编辑器中文件的外观:

PlayerCharacterName.....StrProperty.............CHARACTERNAMEHERE

我可以在不首先将文件反编译为十六进制的情况下找到此字符串吗?是否可以将字符串转换为字节并从那里进行搜索?

这是我目前的代码,但它不适合我的需要,因为字符串的十六进制偏移量对于所有文件都不相同。

Dim pos1 As Long = 864

Dim requiredBytes As Integer = 160
Dim value(0 To requiredBytes - 1) As Byte
Using reader As New BinaryReader(File.Open(fd.FileName, FileMode.Open))
    ' Loop through length of file.
    Dim fileLength As Long = reader.BaseStream.Length
    Dim byteCount As Integer = 0
    reader.BaseStream.Seek(pos1, SeekOrigin.Begin)
    While pos1 < fileLength And byteCount < requiredBytes
        value(byteCount) = reader.ReadByte()
        pos1 += 1
        byteCount += 1
    End While
End Using

字符串之间的点会在文件与文件之间更改十六进制值,但它始终具有相同的点数。 ARK上允许的最大字符名称是24,所以我现在的想法是首先在文件中找到该字符串,开始从该文件的末尾写入128字节的字节。 “PlayerCharacterName ..... StrProperty .............”= 60个字节。这是用户名将开始的位置,最长可达24个字符,即48个字节。然后,我可以过滤掉不属于用户名的剩余字符,并以ASCII格式显示。

我离开这里了吗?

2 个答案:

答案 0 :(得分:0)

由于您可以在文本编辑器中阅读它,因此您只需将其作为文本文件打开即可。

Imports System.IO

Module Module1

    Sub Main()
        Dim line As String = ""
        Using sr As New StreamReader("c:\temp\test.abc")
            While Not sr.EndOfStream
                line = sr.ReadLine()
                If line.StartsWith("PlayerCharacterName") Then
                    Exit While
                End If
            End While
        End Using
        If line = "" Then
            Console.WriteLine("Did not find the name!")
        Else
            Dim s = line.Split("."c)
            Dim name = s.Last()
            Console.WriteLine("Found name ""{0}""", name)
        End If
        Console.Read()
    End Sub

End Module

我在c:\temp\test.abc放了一个文件进行测试。有了这个:

  

5tmodvVa640xaDv0fZ650R85uWo0R   
CqwhYMD9e8h   
FIEeAHhER6Qm2sWY38tKYO   
i7diJRVGiZJUZHx26URbCwsewhby3NhPLMSMOv   
w51Ft4I8aK2bjdu0OmzD3V5tDjlXnCXGfTk1NqAE   
PlayerCharacterName ..... ............. StrProperty CHARACTERNAMEHERE   
J4H73RcfdMVHkLIaXv   
Yo5TCC6MmnkA51BZJcrCkzj62xucQ   
8TR1QfSL1IRdmF2ScjjlokTHYHNa2suBk   
1FBphwSK8aQWdfY1H9tKHSr   
kLbQvNhUhILdcBv1EXXJgwZtQh37JZu2oXoHuCHRf2bpKsKmlZyf055Q5   
ly0WxwtFP47BE0BAVD1sfWBogFR0Qb9r3DKBWiinRk9xLitqT   
g5FgAyCQ5P7v3Z9hz04hQR1KU1SuoscYH7s5SYbHV1mJEnJKIb0

这是我程序的输出:

  

找到名称“CHARACTERNAMEHERE”

答案 1 :(得分:0)

This sounds like a job for a regular expression:

Public Function GetCharacterName(ByVal filePath As String) As String
    Dim exp As New RegEx("PlayerCharacterName.{5}StrProperty.{13}(.{1,24})")
    For Each line As String In File.ReadLines(filePath)
        Dim result = exp.Match(line)
        If result.Success Then
            Return result.Groups(1).Value
        End If
    Next line
    Return Nothing
End Function

My only concern with this is whether I built the expression correctly (I didn't have Visual Studio or real sample data handy) and whether some of the unprintable characters might produce an unexpected newline or multi-byte character.