A way to store data in C# is to write to a binary file. A binary file allows developers to write data types such as int, bool, string, etc to a file. A binary file can then be read to retrieve those values.
Binary files are useful for saving application settings for example in a settings file. They can also save and load data entered by the user during runtime.
Before you even start writing code, add the System.IO namespace to the top. All file writing operations come from that .NET namespace. More specifically, you will be using the FileStream and BinaryWriter classes.
FileStream is the class that either creates or opens a file. In the case of writing binary files, you want to use this to create a new file. Of course FileStream has several options when creating files.
BinaryWriter wraps around FileStream and as you can probably guess, it will write to the stream of that file. BinaryWriter is helpful because it allows programmers to write different C# data types to a file without having to worry about the specific bits. For example, a BinaryWriter can write a string or an uint with the same Write function.
To write more complex data structures to a binary file in C#, you have to convert the data structure to a simpler data type supported by the BinaryWriter. Another way is to write a structure of data in simple data type parts.
Here is a very simple code example:
FileStream stream = new FileStream("C:\\mysecretfile.txt", FileMode.Create); BinaryWriter w = new BinaryWriter(stream); w.Write("hello"); w.Write(5); w.Close();
Notice that the code only closes the BinaryWriter. This is something important to remember. BinaryWriter closes the underlying stream. That means that w.Close() will close the BinaryWriter AND the FileStream. Whether it is convinient or not depends on the situation, but there is no way around it.
Another tip when writing binary files. If you write a list of items, always write the length of the list first and then write the elements. This makes reading the list later a lot easier.
So you used the IO namespace to write a binary file in .NET, how do you read it?
BinaryWriter has a counterpart class called BinaryReader. BinaryReader can read the same data types that BinaryWriter can write. The difference is that while BinaryWriter uses a single Write method, BinaryReader has a separate Read function for each data type. This makes sense since each function returns a different C# type.
But this means that in order to use BinaryReader, the programmer needs to know the structure of the binary file ahead of time. Here is where knowing how many elements of a list to read, for example, is extremely useful.
Whenever BinaryReader calls a read method, it automatically moves the "position" of the reading stream. So if the binary file has 3 integers written in a row, to read them you would call ReadInt32() three times in a row.
It is worth noting that if an improper Read method is called, the stream will throw an exception. Meaning it is always wise to try to catch exceptions when reading binary files.
Also similarly, BinaryReader also closes its underlying stream.
Writing a binary file is a way to store data outside of the .NET application. A binary file can be used to write settings or save data. Creating your own file format might use binary file writing. It is just important to remember the limitations of writing a binary file in C#.