overwrite binary data

  • Thread starter Thread starter Christine Nguyen
  • Start date Start date
C

Christine Nguyen

I've written a program in VB.net. I need to open a file in binary mode and
edit it. I cannot find a way to partially overwrite existing data. There
are certain hex values I need to replace in the file. I am looking at the
System.IO.BinaryWriter class. There only seems to be a way to append or
completely truncate the contents of a file. Does anybody have an idea how I
might accomplish this??

Thanks,
Christine
 
I should state that I actually want to delete parts of the binary data also
(but not all of it).

-Christine
 
Christine Nguyen said:
I should state that I actually want to delete parts of the binary data also
(but not all of it).

-Christine

I don't think you can edit a file directly on the disk.

You have to load the file into an array or other structure using a streamreader,
then operate on it, and then overwrite the file using a streamwriter.
 
Jon Skeet said:
Yes you can - there's no problem doing that. You can't insert or delete
bits from the start of middle of the file, of course, but that's a
different matter.

It seems to me that inserting and deleting is what "editing" is. So if you can't
insert/delete then you can't edit. Or am I missing something?
Note that StreamReader and StreamWriter are meant for text operations,
not the binary operations Christine is interested in.

That's true. I should have said "BinaryReader"..........
 
Chris Devol said:
It seems to me that inserting and deleting is what "editing" is. So
if you can't insert/delete then you can't edit. Or am I missing something?

Yes. You can overwrite. Consider a file which has fixed length records
- it's perfectly possible to overwrite those records with new ones.
Alternatively, you might have something which goes through an assembly
marking all public methods as internal, or vice versa - something which
just requires changing a single byte for each method. No need to read
the whole file in and write it all out again.
 
Hi,

I accomplished using the FileStream Class. It will do binary operations and
insert without copying to a temp file and will delete at the tail end of the
file using Filestream.SetLength.

Thanks,
Christine
 
Back
Top