|
-
Mar 4th, 2008, 10:02 PM
#1
Thread Starter
New Member
[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.
-
Mar 5th, 2008, 04:21 AM
#2
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.
-
Mar 5th, 2008, 04:22 AM
#3
Fanatic Member
Re: [VB.NET 2008] Insert binary data into file
vb Code:
Dim FH As Integer
FH = FreeFile()
FileOpen(FH, "MyFile.abc", OpenMode.Binary, OpenAccess.ReadWrite)
You need to build your own logic for getting the offset based on your file type.
-
Mar 8th, 2008, 05:40 PM
#4
Thread Starter
New Member
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.
-
Mar 8th, 2008, 06:35 PM
#5
Re: [VB.NET 2008] Insert binary data into file
You could open the file once only for read/write access.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|