Mega Code Archive

 
Categories / C# / File Stream
 

FileStream Class supporting both synchronous and asynchronous read and write operations

using System; using System.IO; using System.Text; class Test {     public static void Main()     {         string path = @"c:\MyTest.txt";         using (FileStream fs = File.Create(path))         {             AddText(fs, "This is some text");             AddText(fs, "\r\nand this is on a new line");               for (int i=1;i < 120;i++)             {                 AddText(fs, Convert.ToChar(i).ToString());             }         }         using (FileStream fs = File.OpenRead(path))         {             byte[] b = new byte[1024];             UTF8Encoding temp = new UTF8Encoding(true);             while (fs.Read(b,0,b.Length) > 0)             {                 Console.WriteLine(temp.GetString(b));             }         }     }     private static void AddText(FileStream fs, string value)     {         byte[] info = new UTF8Encoding(true).GetBytes(value);         fs.Write(info, 0, info.Length);     } }