avgcoder
avgcoder

Reputation: 392

Writing bytes to a specific location in a file in vb

I want to be able to seek to a specific byte in a file and then change that byte. I've tried a few things but nothing works.

        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

Using this code I can go to byte 39 but the thing is that everything before this byte turns into 00 and everything after it is deleted.

I have also tried this:

     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()

I have also tried this:

    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()

This is the bytes of the file: http://puu.sh/3nxrd.jpg

Upvotes: 0

Views: 4555

Answers (1)

ChD Computers
ChD Computers

Reputation: 3137

The following code works perfect for me. It first creates a file Test.dat with 100 "A" characters and then goes to position 50 and writes an "F" character leaving the other "A"s intact.

    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

Then with the following code I open the file again go to position 98 and writes a "G", everything works nice.

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

Hope this helps...

Upvotes: 3

Related Questions