NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

FileStream Class

Exposes a Stream around a file, supporting both synchronous and asynchronous methods.

Object
   Stream
      FileStream

[Visual Basic]
Public Class FileStream
   Inherits Stream
[C#]
public class FileStream : Stream
[C++]
public __gc class FileStream : public Stream
[JScript]
public class FileStream extends Stream

Remarks

The File class, used in conjunction with FileStream, is a utility class with static methods primarily for the creation of FileStream objects based on file paths and the standard input, standard output, and standard error devices.

The FileStream class can open a file in one of two modes, either synchronously or asynchronously, with significant performance consequences for the synchronous methods (Read and Write) and the asynchronous methods (BeginRead and BeginWrite). FileStream defaults to opening files synchronously.

The MemoryStream class creates a stream from a byte array and functions similarly to a FileStream.

Requirements

Namespace: System.IO

Assembly: mscorlib.dll

Example [C#]

FileStream objects support random access to files using the Seek method. The Seek method allows the read/write position to be moved to any position within the file. This is done with two parameters, a byte offset and a reference point. The byte offset is relative to the reference point, which can either be the beginning, current position, or end of the underlying file. These reference points are represented by the three properties of the SeekOrigin class. (Disk files always support random access. Note that at the time of construction, the CanSeek property is set to true or false depending on the underlying file type. Specifically, if the underlying file type is FILE_TYPE_DISK, as defined in winbase.h, then the CanSeek property is true, and otherwise, false.)

In the following example, a file is opened, or created if it does not already exist, and information is appended to the end of the file. The contents of the file are then written to standard output for display:

[C#]

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);
     w.Close(); // close the writer and underlying file  
     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 underlying file
     w.Flush();  
  }
  public static void DumpLog (StreamReader r)
  {
// while not at the end of the file
     while (r.Peek() > -1) 
     {
        Console.WriteLine(r.ReadLine());
     }
     r.Close();
  }
}

See Also

FileStream Members | System.IO Namespace | File | FileAccess | FileMode | FileShare