以二进制形式写入和读取值到.exe文件中

时间:2017-07-19 08:53:52

标签: vb.net binary exe

我试图用二进制文件将一些字符串和整数值写入exe文件,然后在需要时以二进制形式读取它们。

我编写了下面的代码,从我的项目的资源中提取exe文件然后以二进制模式打开它并写下我需要的值,然后读取它们,但是问题我面临的是,在我用二进制编写值后,exe不再运行了,我仍然可以读取我在二进制文件中编写的值而不是文件没有运行,如果我只提取exe文件而不写入它,它将正常运行,所以我需要一种方法来使两件事都工作,用二进制写值并仍然使文件正常运行。 "希望有人能帮助我。"

这是我的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
        'Extract the exe file from the resources
        File.WriteAllBytes(SaveFileDialog1.FileName, My.Resources.HelloWorld)

        'Give the file a number to open it in binary mode
        Dim iFr As Short = FreeFile()

        'Open the file in binary mode and write some values on it
        FileOpen(iFr, SaveFileDialog1.FileName, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Default)
        FilePut(iFr, TextBox1.Text & ";" & CheckBox1.CheckState & ";" & CheckBox2.CheckState & ";" & CheckBox3.CheckState)
        FileClose(iFr)
    End If
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim MyString As String
    Dim ReadString As String = My.Computer.FileSystem.SpecialDirectories.Desktop & "\HelloWorld.exe"

    'Give the file a number to open it in binary mode...
    Dim iFr As Short = FreeFile()
    FileOpen(iFr, ReadString, OpenMode.Binary, OpenAccess.Read, OpenShare.Default)

    'Setting the length that i want to read and fill it with spaces.
    MyString = Space(LOF(iFr))

    'Reading the values that i wrote and view them
    FileGet(iFr, MyString)
    FileClose(iFr)
    MsgBox(MyString)
End Sub

更新:

这是不可能的,所以我使用CodeDOM类从我的源代码中重新编译整个exe作为纯文本,我替换了我需要存储它的数据。

0 个答案:

没有答案