Home   Cover Cover Cover Cover
 

Input/Output

Example "Write a simple log into a file"

Shows how a simple log text can be written into a file.

/book/samples/4/IO/Logfile.cs
using System; 
using System.IO; 
using System.Text; // for encoding definitions

public class StreamWriterExample {
  public static void Main() {
    FileStream fs;
    fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Write);
    // Connects a StreamWriter object to the FileStream fs.
    // For the matter of simplicity, this example does not catch any exceptions.
    StreamWriter sw = new StreamWriter(fs, Encoding.Unicode);
    sw.BaseStream.Seek(0, SeekOrigin.End);
    sw.WriteLine("log entry 1");
    sw.WriteLine("log entry 2");
    sw.Close();
    fs.Close();
  }
}




Example "List content of a directory"

In the following example firstly all the subdirectories and then all the files of the logical drive "c:\\" are output to the console.

/book/samples/4/IO/directories.cs
using System; 
using System.IO;

public class DirectoryExample {
  public static void Main() {
    DirectoryInfo dir = Directory.CreateDirectory("c:\\");
    Console.WriteLine("---------- Directories ----------");
    DirectoryInfo[] dirs = dir.GetDirectories();
    foreach (DirectoryInfo d in dirs) Console.WriteLine(d.Name);
    Console.WriteLine ("---------- Files ----------");
    FileInfo[] files = dir.GetFiles();
    foreach (FileInfo f in files) Console.WriteLine(f.Name);
  }
}




Example "Monitoring the file system"

The following example shows a simple program that monitors all changes to files on the logical drive "c:\\" and creates an output on the console. The class FileSystemWatcher supports a variety of results that report information about changes in file system to registered observers:

/book/samples/4/IO/monitoring.cs
using System; 
using System.IO;
public class WatcherEventExample {
  public static void Changed(object sender, FileSystemEventArgs args) {
    Console.WriteLine("Changed -> {0}", args.Name);
  }
  public static void Created(object sender, FileSystemEventArgs args) {
    Console.WriteLine("Created -> {0}", args.Name);
  }
  public static void Deleted(object sender, FileSystemEventArgs args) {
    Console.WriteLine("Deleted -> {0}", args.Name);
  }
  public static void Renamed(object sender, RenamedEventArgs args) {
    Console.WriteLine("Renamed -> {0}", args.Name);
  }
  public static void Main() {
    FileSystemWatcher fsw = new FileSystemWatcher("c:\\");
    //----- register all event handlers:
    fsw.Changed += new FileSystemEventHandler(Changed);
    fsw.Created += new FileSystemEventHandler(Created);
    fsw.Deleted += new FileSystemEventHandler(Deleted);
    fsw.Renamed += new RenamedEventHandler(Renamed);
    fsw.IncludeSubdirectories = true;
    fsw.EnableRaisingEvents = true;
    fsw.Filter = "*.*";
    while ( ... ) fsw.WaitForChanged(WatcherChangeTypes.All);
  }
}




Example "Isolated storage"

The two classes IsolatedStorageFile and IsolatedStorageFileStream (derived from IsolatedStorage and FileStream) can be used to set up an isolated storage area, as the following example demonstrates:

/book/samples/4/IO/isolated.cs
using System; 
using System.IO; 
using System.IO.IsolatedStorage;

public class IsolatedStorageExample {
  public static void Main() {
    // create an isolated file
    IsolatedStorageFile iso =  IsolatedStorageFile.GetStore
        (IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
    // create a data stream that writes to the isolated file
    IsolatedStorageFileStream ifs = new IsolatedStorageFileStream
        ("test", FileMode.OpenOrCreate, iso);
    // redirect output to StreamWrite to be able to write strings more conveniently
    StreamWriter sw = new StreamWriter(ifs);
    // store a test string in the isolated storage area
    sw.Write("This is a test.");
    // close all output streams (with implicit Flush of data buffer)
    sw.Close(); ifs.Close(); iso.Close();
  }
}