将字节写入vb中文件中的特定位置

时间:2013-06-24 22:49:28

标签: vb.net binary byte

我希望能够搜索文件中的特定字节,然后更改该字节。我尝试了一些但没有任何作用。

        Const fileName As String = "Option.mco"

    Using writer As BinaryWriter = New BinaryWriter(File.Open(fileName, FileMode.Create))
        writer.Seek(39, SeekOrigin.Begin)
        writer.Write(1600)
    End Using

使用这段代码,我可以转到第39栏,但事实是这个字节之前的所有内容都变为00,并且删除后的所有内容。

我也试过这个:

     Dim fLocation = ("Option.mco")
    Dim fInfo As New FileInfo(fLocation)
    Dim numBytes As Long = fInfo.Length
    Dim fStream As New FileStream(fLocation, FileMode.Open, FileAccess.Read)
    Dim br As New BinaryReader(fStream)
    Dim data As Byte() = br.ReadBytes(CInt(numBytes))
    Dim fs As New FileStream(fLocation, FileMode.Open, FileAccess.Read)


    ' Show the number of bytes in the array.
    'Label1.Text = Convert.ToString(data.Length)

    'Dim writeStream As FileStream
    'writeStream = New FileStream(fLocation, FileMode.Create)
    'Dim writeBinay As New BinaryWriter(writeStream)

    Dim value As Short = 1600
    Dim value2 As Short = 900
    Dim dataArray(1600) As Byte
    fs.Seek(39, SeekOrigin.Begin)
    fs.Write(1600)
    'writeBinay.Write(value)
    fs.Seek(43, SeekOrigin.Begin)
    'writeBinay.Write(value2)
    fs.WriteByte(value2)
    'writeBinay.Close()

    br.Close()

    fStream.Close()

我也试过这个:

    Dim fLocation = ("Option.mco")

    Dim writeStream As New FileStream(fLocation, FileMode.Create)
    Dim writeBinay As New BinaryWriter(writeStream)

    Dim value As Short = 1600
    Dim value2 As Short = 900
    writeBinay.Seek(39, SeekOrigin.Begin)
    writeBinay.Write(value)
    writeBinay.Seek(43, SeekOrigin.Begin)
    writeBinay.Write(value2)
    writeBinay.Close()

这是文件的字节: http://puu.sh/3nxrd.jpg

1 个答案:

答案 0 :(得分:3)

以下代码对我来说非常适合。它首先创建一个文件Test.dat,其中包含100个“A”字符,然后转到位置50并写入“F”字符,而其他“A”字符保持不变。

    Sub Main()
    Const fileName As String = "Test.dat"

    Dim dataArray(100) As Byte
    For i As Integer = 0 To dataArray.Length - 1
        dataArray(i) = 65
    Next

    Dim fileStream As IO.FileStream = _
        New IO.FileStream(fileName, IO.FileMode.Create)
    Try

        ' Write the data to the file, byte by byte.
        For i As Integer = 0 To dataArray.Length - 1
            fileStream.WriteByte(dataArray(i))
        Next i

        ' Set the stream position to the desired location of the stream.
        fileStream.Seek(50, IO.SeekOrigin.Begin)


        'Write Character ASCII 70
        fileStream.WriteByte(70)

        ' Set the stream position to the begining of the stream.
        fileStream.Seek(0, IO.SeekOrigin.Begin)
    Finally
        fileStream.Close()
    End Try
End Sub

然后使用下面的代码我再次打开文件转到位置98并写一个“G”,一切都很好。

Sub Main()
    Const fileName As String = "Test.dat"

    Dim fileStream As IO.FileStream = _
        New IO.FileStream(fileName, IO.FileMode.Open)
    Try
        ' Set the stream position to the desired location of the stream.
        fileStream.Seek(98, IO.SeekOrigin.Begin)

        'Write Character ASCII 71
        fileStream.WriteByte(71)
    Finally
        fileStream.Close()
    End Try
End Sub

希望这会有所帮助......