From Murach c# 2005 - ch. 22 Directory -Exists(path) - CreateDirectory(path) - Delete(path) File - Exists(path) - Delete(path) - Copy(source, dest) - Move(source, dest) Path string dir = @c"\temp\"; if (!Directory.Exists(dir)) Directory.Create(dir) string path = dir + "Products.txt"; if (File.Exists(path)) File.Delete(path); -- Stream - flow of data from one location to another text or binary streams FileStream StreamReader StreamWriter BinaryReader BinaryWriter FileMode - Append (creates if doesn't exist) - Create (overwritten) - CreateNew (ex if exist) - Open (ex if exist) - OpenOrCreate - Truncate FileAccess - Read - ReadWrite (def) - Write FileShare - None - Read (def) - ReadWrite - Write FileStream - Close //writing string path = @"c:\temp\Products.txt"; FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); //reading string path = @"c:\temp\Products.txt"; FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); ---------- Exceptions ---------- IOException (base class) DirectoryNotFoundException FileNotFoundException EndOfStreamException (read beyond end of stream) -- string dirPath = @"c:\temp\" string filePath = dirPath + "Products.txt"; FileStream fs = null; try { fs = new FileStream(filePath, FileMode.Open); } catch(FileNotFoundException) { MesssageBox.Show(filePath + " not found.", "File Not Found"); } catch(DirectoryNotFoundException) { MesssageBox.Show(dirPath + " not found.", "File Not Found"); } catch(IOException ioe) { MesssageBox.Show(ioe.Message, "IO Exception"); } finally { if (fs != null) fs.Close(); } --- StreamWriter - Write(data) - WriteLine(data) - Close() StreamWriter textOut = new StreamWriter( new FileStream(path, FileMode.Create, FileAccess.Write)); foreach (Product product in products) { textOut.Write( product.Code + "|"); textOut.Write( product.Description + "|"); textOut.WriteLine( product.Price); } textOut.Close(); --- StreamReader - Peek() //reads w/o advancing to next position in stream - Read() // next char - ReadLine() // next line - ReadToEnd() // current position to the end, usually whole file - Close() StreamReader textIn = new StreamReader( new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)); List products = new List(); while (textIn.Peek() != -1) { string row = textIn.ReadLine(); string [] columns = row.Split('|'); Product product = new Product(); product.Code = columns[0]; product.Description = columns[1]; product.Price = convert.ToDecimal(columns[2]); products.Add(product); } textIn.Close(); --- using System; using System.IO; using System.Collections.Generic; namespace ProductMaintenance { public class ProductDB { private const string dir = @"c:\temp\" private const string file = dir + "Products.txt" public static List GetProducts() { if (!Directory.Exists(dir)) Directory.Create(dir); StreamReader textIn = new StreamReader( new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)); List products = new List(); while (textIn.Peek() != -1) { string row = textIn.ReadLine(); string [] columns = row.Split('|'); Product product = new Product(); product.Code = columns[0]; product.Description = columns[1]; product.Price = convert.ToDecimal(columns[2]); products.Add(product); } textIn.Close(); return products; } public static void SaveProducts(List products) { StreamWriter textOut = new StreamWriter( new FileStream(path, FileMode.Create, FileAccess.Write)); foreach (Product product in products) { textOut.Write( product.Code + "|"); textOut.Write( product.Description + "|"); textOut.WriteLine( product.Price); } textOut.Close(); } }// end class ProductDB } // end namespace ProductMaintenance ------ binary ------ BinaryWriter - Write(data) - Close() BinaryWriter binaryOut = new BinaryWriter( new FileStream(path, FileMode.Create, FileAccess.Write)); foreach (Product product in products) { binaryOut.Write(product.Code); binaryOut.Write(product.Description); binaryOut.Write(product.Price); } binaryOut.Close(); --- BinaryReader - PeekChar() //reads w/o advancing to next position - Read() // next char - advance to next position - ReadBoolean() // boolean - advance one byte - ReadByte() // byte - advance one byte - ReadChar() // char - advance one Char - ReadDecimal() // decimal - advance 16 bytes - ReadInt32() // 4-byte signed int - advance 4 bytes - ReadString() // string - advances by number of chanacters in the string - Close() BinaryReader binaryIn = new BinaryReader( new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)); List products = new List(); while (binaryIn.PeekChar() != -1) { Product product = new Product(); product.Code = binaryIn.ReadString(); product.Description = binaryIn.ReadString(); product.Price = binaryIn.ReadDecimal(); products.Add(product); } binaryIn.Close(); --- using System; using System.IO; using System.Collections.Generic; namespace ProductMaintenance { public class ProductDB { private const string dir = @"c:\temp\" private const string file = dir + "Products.dat" public static List GetProducts() { if (!Directory.Exists(dir)) Directory.Create(dir); BinaryReader binaryIn = new BinaryReader( new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)); List products = new List(); while (binaryIn.PeekChar() != -1) { Product product = new Product(); product.Code = binaryIn.ReadString(); product.Description = binaryIn.ReadString(); product.Price = binaryIn.ReadDecimal(); products.Add(product); } binaryIn.Close(); return products; } public static void SaveProducts(List products) { BinaryWriter binaryOut = new BinaryWriter( new FileStream(path, FileMode.Create, FileAccess.Write)); foreach (Product product in products) { binaryOut.Write(product.Code); binaryOut.Write(product.Description); binaryOut.Write(product.Price); } binaryOut.Close(); } }// end class ProductDB } // end namespace ProductMaintenance