Questions tagged [binarywriter]
170 questions
30
votes
4 answers
Why a BinaryWriter closes the outer Stream on disposal, and how to prevent that? (.NET C#)
I have one method that receives a Stream to write on it using a BinaryWriter. But when I dispose this BinaryWriter it also closes the stream. Can I leave it undisposed so I can leave my stream open?

Jader Dias
- 88,211
- 155
- 421
- 625
19
votes
3 answers
C# and .NET: How to serialize a structure into a byte[] array, using BinaryWriter?
How to serialize a rather complex structure into a byte[] array, using BinaryWriter?
Update:
For this to work, every structure (and sub-structure?) must be decorated with the [Serializable] attribute.
I do not need to implement the ISerializable…

Contango
- 76,540
- 58
- 260
- 305
15
votes
3 answers
C# BinaryWriter - and endianness
I am using BinaryWriter in my code, here is my code:
static void Main(string[] args)
{
FileInfo file = new FileInfo(@"F:\testfile");
if (file.Exists) file.Delete();
using (BinaryWriter bw = new BinaryWriter(file.Create()))
{
…

Eddy Lin
- 475
- 2
- 6
- 19
14
votes
9 answers
Why does BinaryWriter prepend gibberish to the start of a stream? How do you avoid it?
I'm debugging some issues with writing pieces of an object to a file and I've gotten down to the base case of just opening the file and writing "TEST" in it. I'm doing this by something like:
static FileStream fs;
static BinaryWriter w;
fs = new…

Chris
- 21,549
- 25
- 71
- 99
10
votes
2 answers
Get Encoding of BinaryReader/Writer?
The .NET BinaryReader/BinaryWriter classes can be constructed with specifying an Encoding to use for String-related operations.
I was implementing custom string formats with extension methods, but would yet implement them in a way they respect the…

Ray
- 7,940
- 7
- 58
- 90
9
votes
4 answers
Bit-Based BinaryWriter in C#
I'm working on a bit-based B/W/Greyscale Pre-Compiled font format, and was having issues with either reading or writing the format, (I've not been able to determine where the issue was. (I do have a B/W bit-based version working, but an Aliased font…

Orvid King
- 1,188
- 10
- 16
9
votes
3 answers
Using BinaryWriter or BinaryReader in async code
I have a list of float to write to a file. The code below does the thing but it is synchronous.
List samples = GetSamples();
using (FileStream stream = File.OpenWrite("somefile.bin"))
using (BinaryWriter binaryWriter = new…

Yusuf Tarık Günaydın
- 3,016
- 2
- 27
- 41
8
votes
2 answers
How To Write A String Of Binary To File C#
I Have A String Of Binary Number Like temp = "0101110011" And I Want To Save That As File this Temp Have 10 char And How Can I Save This string To file With 10 Bit Length ?
void Save_Data(string temp)
{
bool[] BoolArray = new bool[temp.Length];
…

InvBoy
- 79
- 1
- 1
- 6
7
votes
1 answer
BinaryWriter.Write(byte value) issue when value > 127
I'm writing raw byte values to a file:
When values are <= 127, everything is ok.
But if a byte is > 127, it gets all messsed up.
I've already tried changing encoding format and such, with no success.
public static void Generate()
{
var…

Rafael Moura
- 81
- 1
- 6
6
votes
3 answers
How do I write a list using BinaryWriter?
I want to use a generic WriteList(List value) function to write a List using the BinaryWriter. Here is the code I am using:
public void WriteList(List value)
{
for (int i = 0; i < value.Count; i++)
{
_writer.Write(value[i]);
…

LunchMarble
- 5,079
- 9
- 64
- 94
6
votes
2 answers
Difference between BinaryWriter and BinaryFormatter.Serialize?
I'm new to object serialization, and in the course of my learning how to read from and write to a file (deserialize and serialize) using BinaryFormatter, I came across BinaryReader and BinaryWriter, which seemed to be doing the same thing.
Is there…

AustinC
- 169
- 2
- 7
6
votes
1 answer
Size of types when writing into BinaryWriter
Size of each character in ASCII (StreamWriter) takes 1 byte whether its a number or character.
Similarly what will be the size of each character, integer in binary? (BinaryWriter). Can some one explain in brief?

hitesh jain
- 119
- 5
5
votes
1 answer
Binary Reader and Writer open at same time?
I'm writing code that deals with a file that uses hashes. I need to read a chunk, then hash it, then write it, then read another chunk, etc.
In other words, I need to do a lot of reading and writing. I'm sure this is really simple, but I just…

mowwwalker
- 16,634
- 25
- 104
- 157
5
votes
1 answer
Using BinaryWriter on an Object
My application is a small C# database, and I'm using BinaryWriter to save the database to a file which is working fine with the basic types such as bool, uint32 etc.
Although I've got a variable that is of type Object (allowing the user to store any…

R4D4
- 1,382
- 2
- 13
- 34
5
votes
1 answer
Faster way to write image to Process.StandardInput.BaseStream
Im trying to send a lot of desktop captured images to an encoders (FFmpeg) stdin.
The following code example works.
the CaptureScreen() function provides an image in 5-10 ms.
If I save the image in a MemoryStream it takes almost no time.
But I can…

Hasibii
- 51
- 1
- 2