using System; using System.IO; public class IOExample { static void Main() { FileStream fs; StreamReader srIn; try { fs = new FileStream("practice.txt", FileMode.Open ); srIn = new StreamReader(fs); string line = srIn.ReadLine(); Console.WriteLine("line = "+line); srIn.Close(); } catch (IOException ioe) { Console.WriteLine("IOException occurred: "+ioe.Message); } } }
Reads strings from a file created in a text editor
using System; using System.IO; public class FileReadStrings { public static void Main( ) { String line; StreamReader f = new StreamReader("test.data"); while((line=f.ReadLine()) != null) Console.WriteLine(line); f.Close(); } } //File: test.data /* 4665 Street|Toronto|ON|90048 */
Reads lines separated by vertical bars
using System; using System.IO; public class FileReadAddresses { public static void Main( ) { String line; String street, city, state, zip; StreamReader f = new StreamReader("test.data"); while ((line=f.ReadLine())!= null){ String[] strings = line.Split(new char[]{'|'}); if (strings.Length == 4) { street = strings[0]; city = strings[1]; state = strings[2]; zip = strings[3]; Console.WriteLine(street); Console.WriteLine(city); Console.WriteLine(state); Console.WriteLine(zip); } } f.Close(); } } //File: test.data /* 4665 Street|Toronto|ON|90048 */
FileStream with FileMode.Create and FileMode.Open
using System; using System.IO; using System.Text; class MainClass { static void Main() { using (FileStream fs = new FileStream("test.txt", FileMode.Create)) { using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) { w.WriteLine(124.23M); w.WriteLine("Test string"); w.WriteLine('!'); } } Console.WriteLine("Press Enter to read the information."); Console.ReadLine(); // Open the file in read-only mode. using (FileStream fs = new FileStream("test.txt", FileMode.Open)) { using (StreamReader r = new StreamReader(fs, Encoding.UTF8)) { // Read the data and convert it to the appropriate data type. Console.WriteLine(Decimal.Parse(r.ReadLine())); Console.WriteLine(r.ReadLine()); Console.WriteLine(Char.Parse(r.ReadLine())); } } } }
Set File IO Permission to c:
using System; using System.Collections.Generic; using System.Text; using System.Security.Permissions; class Program { static void Main(string[] args) { MyClass.Method(); } } [FileIOPermission(SecurityAction.Assert, Read = "C:/")] class MyClass { public static void Method() { // implementation goes here } }
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()); } } } } }
Uses methods in the File class to check the status of a file
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // Status.cs -- Uses methods in the File class to check the status of a file. // // Compile this program with the following command line // C:>csc Status.cs using System; using System.IO; namespace nsStreams { public class Status { static public void Main (string [] args) { if (args.Length == 0) { Console.WriteLine ("Please enter a file name"); return; } if (!File.Exists (args[0])) { Console.WriteLine (args[0] + " does not exist"); return; } DateTime created = File.GetCreationTime (args[0]); DateTime accessed = File.GetLastAccessTime (args[0]); DateTime written = File.GetLastWriteTime (args[0]); Console.WriteLine ("File " + args[0] + ":"); string str = created.ToString(); int index = str.IndexOf (" "); Console.WriteLine (" Created on " + str.Substring (0, index) + " at " + str.Substring (index + 1)); str = accessed.ToString(); index = str.IndexOf (" "); Console.WriteLine (" Last accessed on " + str.Substring (0, index) + " at " + str.Substring (index + 1)); str = written.ToString(); index = str.IndexOf (" "); Console.WriteLine (" Last written on " + str.Substring (0, index) + " at " + str.Substring (index + 1)); } } }