Results 1 to 5 of 5

Thread: [VB.NET 2008] Insert binary data into file

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    10

    [VB.NET 2008] Insert binary data into file

    Hi, I'm trying to insert varying amounts of bytes into a file at a certain offset, but I'm a little lost. I'm assuming there would be some sort of shift and add, but I'm unsure on a proper method. Any help would be appreciated.

    Note: I don't want to overwrite any data, just insert a specified number of bytes.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [VB.NET 2008] Insert binary data into file

    You can create a FileStream and call the Seek method to move the file pointer to a specific position. That said, I don't it's possible to insert data into a file like that. I think it would require reading the existing contents, then writing the contents out again in two parts, with the new data written in between.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member pvbangera's Avatar
    Join Date
    Sep 2001
    Location
    Mumbai, India
    Posts
    961

    Re: [VB.NET 2008] Insert binary data into file

    vb Code:
    1. Dim FH As Integer
    2.         FH = FreeFile()
    3.         FileOpen(FH, "MyFile.abc", OpenMode.Binary, OpenAccess.ReadWrite)

    You need to build your own logic for getting the offset based on your file type.
    Microsoft Techie

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    10

    Re: [VB.NET 2008] Insert binary data into file

    Thanks for the responses. I sort of got what I wanted by doing this:

    Code:
    Public Sub Insert(ByVal StartOffset As Int32, ByVal Size As Int32)
            Dim br As New IO.BinaryReader(New IO.FileStream(MAP.Path, IO.FileMode.Open, IO.FileAccess.Read))
            Dim block As Byte()
            Dim bytesleft As Integer = br.BaseStream.Length - StartOffset
            br.BaseStream.Position = StartOffset
            block = br.ReadBytes(bytesleft)
            br.Close()
            Dim bw As New IO.BinaryWriter(New IO.FileStream(MAP.Path, IO.FileMode.Open, IO.FileAccess.Write))
            Dim zero As Byte = 0
            For i As Integer = 0 To Size - 1
                bw.BaseStream.Position = StartOffset + i
                bw.Write(zero)
            Next
            bw.BaseStream.Position = StartOffset + Size
            bw.Write(block)
            bw.Close()
        End Sub

    Is there a smarter/faster way of doing this? This code is pretty sluggish... Thanks in advance.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [VB.NET 2008] Insert binary data into file

    You could open the file once only for read/write access.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width