Writing Binary files

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know how I can write a binary file? Basically I want to read in
a file that is an exe and write it back out bit by bit so I can add some
header info.

Thanks,
Michael
 
Michael,

You can use the FileStream class for this. Just open the file and read
the bytes from the stream, and then re-write to a new FileStream instance,
modifying the bytes as you move through the file.
 
Can you give me an example?



Nicholas Paldino said:
Michael,

You can use the FileStream class for this. Just open the file and read
the bytes from the stream, and then re-write to a new FileStream instance,
modifying the bytes as you move through the file.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Does anyone know how I can write a binary file? Basically I want to read
in
a file that is an exe and write it back out bit by bit so I can add some
header info.

Thanks,
Michael
 
Michael,

The documentation for the FileStream class should have plenty of
examples. You simply have to read chunks of bytes from one stream, and then
write them to another, using the Read and the Write methods on the
FileStream.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Can you give me an example?



Nicholas Paldino said:
Michael,

You can use the FileStream class for this. Just open the file and
read
the bytes from the stream, and then re-write to a new FileStream
instance,
modifying the bytes as you move through the file.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michael said:
Does anyone know how I can write a binary file? Basically I want to
read
in
a file that is an exe and write it back out bit by bit so I can add
some
header info.

Thanks,
Michael
 
Use a System.IO.BinaryWriter. The constructor for it takes a stream which
you create via one of the methods for opening a file for writing. The
BinaryWriter contains the methods used to do the writing.

See http://msdn2.microsoft.com/en-us/library/system.io.binarywriter.aspx for
more details and sample code.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
Back
Top