Create a file stream with new FileStream(“test.bin”, FileMode.Create)

   
 

using System;
using System.IO;

class MainClass {
    static void Main() {
        using (FileStream fs = new FileStream("test.bin", FileMode.Create)) {
            using (BinaryWriter w = new BinaryWriter(fs)) {
                w.Write(124.23M);
                w.Write("Tstring");
                w.Write("Tstring 2");
                w.Write('!');
            }
        }
        Console.WriteLine("Press Enter to read the information.");
        Console.ReadLine();

        using (FileStream fs = new FileStream("test.bin", FileMode.Open)) {
            using (StreamReader sr = new StreamReader(fs)) {
                Console.WriteLine(sr.ReadToEnd());
                Console.WriteLine();

                fs.Position = 0;
                using (BinaryReader br = new BinaryReader(fs)) {
                    Console.WriteLine(br.ReadDecimal());
                    Console.WriteLine(br.ReadString());
                    Console.WriteLine(br.ReadString());
                    Console.WriteLine(br.ReadChar());
                }
            }
        }
    }
}

    


This entry was posted in File Stream. Bookmark the permalink.