# VBForums CodeBank > CodeBank - C# >  Reading and Writing Binary Data Using the BinaryReader and BinaryWriter Classes

## jmcilhinney

VB version here.

Binary primitives can be written to and read from files directly using the BinaryWriter and BinaryReader classes.  You can use these classes to create your own binary file format for your application.

Below is an example that writes some binary primitives to a new file, then reads those values back again and displays them.  Each value in the file is preceded by a byte whose value indicates the data type of the following value.  You can change the types and values in the writing section of the code below to whatever you want and you'll find that, as long as you specify the correct data type for each value you write, the reading section will always display the correct output.

Create a new Console Application project and then copy the following code over the existing code and then just run the project.

```
using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            const string filePath = @"C:\BinaryTest.bin";

            using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
            {
                // Write a 32-bit integer.
                writer.Write((byte)DataType.Int32);
                writer.Write(12345);
                // Write a double-preceision floating-point value.
                writer.Write((byte)DataType.Double);
                writer.Write(222.333);
                // Write a string.
                writer.Write((byte)DataType.String);
                writer.Write("Hello World");
                // Write a 32-bit integer.
                writer.Write((byte)DataType.Int32);
                writer.Write(98765);
            }

            DataType type;

            using (BinaryReader reader = new BinaryReader(File.OpenRead(filePath)))
            {
                // Keep reading while there is data available.
                while (reader.PeekChar() != -1)
                {
                    // Read the data type of the following value.
                    type = (DataType)reader.ReadByte();

                    Console.Write("{0}: ", type);

                    // Read the next value based on its data type.
                    switch (type)
                    {
                        case DataType.Byte:
                            Console.WriteLine(reader.ReadByte().ToString());
                            break;
                        case DataType.Int16:
                            Console.WriteLine(reader.ReadInt16().ToString());
                            break;
                        case DataType.Int32:
                            Console.WriteLine(reader.ReadInt32().ToString());
                            break;
                        case DataType.Int64:
                            Console.WriteLine(reader.ReadInt64().ToString());
                            break;
                        case DataType.Single:
                            Console.WriteLine(reader.ReadSingle().ToString());
                            break;
                        case DataType.Double:
                            Console.WriteLine(reader.ReadDouble().ToString());
                            break;
                        case DataType.Decimal:
                            Console.WriteLine(reader.ReadDecimal().ToString());
                            break;
                        case DataType.String:
                            Console.WriteLine(reader.ReadString());
                            break;
                        case DataType.Char:
                            Console.WriteLine(reader.ReadChar().ToString());
                            break;
                        case DataType.Boolean:
                            Console.WriteLine(reader.ReadBoolean().ToString());
                            break;
                        default:
                            break;
                    }
                }
            }

            Console.ReadLine();
        }
    }

    public enum DataType : byte
    {
        Byte,
        Int16,
        Int32,
        Int64,
        Single,
        Double,
        Decimal,
        String,
        Char,
        Boolean
    }
}
```

----------

