The following Managed Extensions for C++ example opens a file or creates a file if it does not already exist, and appends information to the end of the file. The contents of the file are then written to standard output for display.
using System; using System.IO; class Directory { public static void Main(String[] args) { FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Write); // Create a Char writer. StreamWriter w = new StreamWriter(fs); // Set the file pointer to the end. w.BaseStream.Seek(0, SeekOrigin.End); Log ("Test1", w); Log ("Test2", w); // Close the writer and underlying file. w.Close(); fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Read); // Create a Char reader. StreamReader r = new StreamReader(fs); // Set the file pointer to the beginning. r.BaseStream.Seek(0, SeekOrigin.Begin); DumpLog (r); } public static void Log (String logMessage, StreamWriter w) { w.Write("\r\nLog Entry : "); w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString()); w.WriteLine(" :"); w.WriteLine(" :{0}", logMessage); w.WriteLine ("-------------------------------"); // Update the underlying file. w.Flush(); } public static void DumpLog (StreamReader r) { // While not at the end of the file, read and write lines. while (r.Peek() > -1) { Console.WriteLine(r.ReadLine()); } r.Close(); }