Create a file stream with new FileStream("test.bin", FileMode.Create) : FileStream : File Stream C# Source Code


Custom Search

C# Source Code » File Stream » FileStream »

 

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());
                }
            }
        }
    }
}

 
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo File Stream
» FileStream