The following example writes some data to a file.
using System; using System.IO; public class DataToFile { public static void Main(String[] args) { int myInt = 5; File f = new File("MyData.data"); Stream s = f.Open(FileMode.OpenOrCreate); BinaryWriter d = new BinaryWriter (s); d.Write(myInt); s.Close(); } }
This code creates a file object that points to MyData.data. Next, a FileStream is created through a call to File.Open, returning a FileStream object. A BinaryWriter is then created and passed the FileStream as its argument. BinaryWriter.Write writes the integer myInt to the file.