BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.IO.Stream

Exposes a around a file, supporting both synchronous and asynchronous read and write operations.

is used for reading and writing files on a file system, as well as other file-related operating system handles such as pipes, standard input, standard output. buffers input and output for better performance.

The class can open a file in one of two modes, either synchronously or asynchronously, with significant performance consequences for the synchronous methods ( and ) and the asynchronous methods ( and ). Both sets of methods will work in either mode; however, the mode will affect the performance of these methods. defaults to opening files synchronously, but provides a constructor to open files asynchronously.

When accessing files, a security check is performed when the file is created or opened. The security check is typically not done again unless the file is closed and reopened. Checking permissions when the file is first accessed minimizes the impact of the security check on application performance (since opening a file happens once, while reading and writing can happen multiple times). Note that if an opened file is passed to an untrusted caller, the security system can, but is not required to prevent the caller from accessing the file.

objects support random access to files using the method, and the properties of instances encapsulating files are set to . The method allows the read/write position to be moved to any position within the file. This is done with byte offset reference point parameters. The byte offset is relative to the seek reference point, which can be the beginning, the current position, or the end of the underlying file, as represented by the three values of the enumeration.

If a encapsulates a device that does not support seeking, its property is . For additional information, see .

The class provides methods for the creation of objects based on file paths. The class creates a stream from a byte array and functions similarly to a .

The following example demonstrates the use of a object.

`using System; using System.IO; class Directory { public static void Main(String[] args) { FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter w = new StreamWriter(fs); w.BaseStream.Seek(0, SeekOrigin.End); // Set the file pointer to the end. Log ("Test1", w); Log ("Test2", w); w.Close(); // Close the writer and underlying file. fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Read); StreamReader r = new StreamReader(fs); r.BaseStream.Seek(0, SeekOrigin.Begin); DumpLog (r); } public static void Log (String logMessage, StreamWriter w) { w.Write("Log Entry : "); w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString()); w.WriteLine(":"); w.WriteLine(":{0}", logMessage); w.WriteLine ("-------------------------------"); w.Flush(); } public static void DumpLog (StreamReader r) { while (r.Peek() > -1) { // While not at the end of the file, write to standard output. Console.WriteLine(r.ReadLine()); } r.Close(); } }`

Some example output is

Log Entry : 9:26:21 AM Friday, July 06, 2001

:

:Test1

-------------------------------

Log Entry : 9:26:21 AM Friday, July 06, 2001

:

:Test2

-------------------------------

- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Obsolete("Use FileStream(SafeFileHandle handle, FileAccess access) instead")
- Constructor
  A containing the relative or absolute path for the file that the current object will encapsulate.
  A value that determines how to open or create the file.

  Constructs and initializes a new instance of the class with the specified path and creation mode.

  This constructor sets access to the file, and the and properties of the current instance are set to .

  is not required to be a file stored on disk; it can be any part of a system that supports access via streams. For example, depending on the system, this class might be able to access a physical device.

  is for all objects that encapsulate files. If specifies a device that does not support seeking, the property of the resulting is required to be . For additional information, see .

  Requests to open the file for writing by the current or another thread will fail until the object has been closed. Read attempts will succeed.

  is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
  is .
  The caller does not have the required permission.

  is or , but the specified file cannot be found. If a different mode is specified and the file cannot be found, a new one is created.

  An I/O error occurred, such as specifying when the file specified by already exists.
  The directory information specified in does not exist.
  The length of or the absolute path information for exceeds the system-defined maximum length.

  contains an invalid value.

  Requires permission to read, write, and append to files. See , , and .
  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Obsolete("Use FileStream(SafeFileHandle handle, FileAccess access) instead")
- Constructor
  A containing the relative or absolute path for the file that the current object will encapsulate.
  A value that determines how to open or create the file.
  A value that determines how the file can be accessed by the object. This parameter is used to specify the initial values of the and properties.

  Constructs and initializes a new instance of the class with the specified path, creation mode, and access type.

  This constructor sets read/write access to the file. Requests to open the file for writing by the current or another thread will fail until the object has been closed. Read attempts will succeed.

  is not required to be a file stored on disk; it can be any part of a system that supports access via streams. For example, depending on the system, this class might be able to access a physical device.

  is for all objects that encapsulate files. If indicates a device that does not support seeking, the property on the resulting is required to be . For additional information, see .

  is .

  is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.

  -or-

  specified and specified , , or .

  is or , but the specified file was not found. If a different mode is specified and the file was not found, a new one is created.

  An I/O error occurred, such as specifying when the file specified by already exists.
  The caller does not have the required permission.
  The directory information specified by does not exist.
  specified a read-only file and is not , or specified a directory.
  The length of or the absolute path information for exceeds the system-defined maximum length.

  or contain an invalid value.

  Requires permission to read, write, and append to files. See , , and .
  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Obsolete("Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize) instead")
- Constructor
  A containing relative or absolute path for the file that the current object will encapsulate.
  A value that determines how to open or create the file.
  A value that determines how the file can be accessed by the object. This parameter is used to specify the initial values of the and properties. For additional information, see and .
  A value that determines how the file will be shared by processes.

  Constructs and initializes a new instance of the class with the specified path, creation mode, access type, and sharing permission.

  This constructor sets read/write access to the file.

  is not required to be a file stored on disk; it can be any part of a system that supports access via streams. For example, depending on the system, this class might be able to access a physical device.

  is for all objects that encapsulate files. If indicates a device that does not support seeking, the property on the resulting is required to be . For additional information, see .

  is .
  is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.

  is or , but the specified file cannot be found. If a different mode is specified and the file cannot be found, a new one is created.

  An I/O error occurred, such as specifying and the file specified by already exists.
  The caller does not have the required permission.
  The directory information specified by does not exist.
  The requested is not permitted by the operating system for the specified .
  The length of or the absolute path information for exceeds the system-defined maximum length.

  , , or contains an invalid value.

  Requires permission to read, write, and append to files. See , , and .
  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Obsolete("Use FileStream(SafeFileHandle handle, FileAccess access, int bufferSize, bool isAsync) instead")
- Constructor
  A containing the relative or absolute path for the file that the current object will encapsulate.
  A constant that determines how to open or create the file.
  A value that determines how the file can be accessed by the object. This parameter is used to specify the initial values of the and properties. For additional information, see and .
  A constant that determines how the file will be shared by processes.
  A containing the desired buffer size in bytes.

  Constructs and initializes a new instance of the class.

  is not required to be a file stored on disk; it can be any part of a system that supports access via streams. For example, depending on the system, this class might be able to access a physical device.

  is for all objects that encapsulate files. If indicates a device that does not support seeking, the property on the resulting is required to be . For additional information, see .

  The parameter is .
  is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.

  is less than or equal to zero.

  -or-

  , , or contain an invalid value.

  is or , but the specified file cannot be found. If a different mode is specified and the file cannot be found, a new one is created.

  An I/O error occurred, such as specifying and the file specified by already exists.
  The caller does not have the required permission.
  The directory information specified in does not exist.
  The requested is not permitted by the operating system for the specified .
  The length of or the absolute path information for exceeds the system-defined maximum length.
  Requires permission to read, write, and append to files. See , , and .
  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Constructor
  A containing the relative or absolute path for the file that the new object will encapsulate.
  A value that determines how to open or create the file.
  A value that determines how the file can be accessed by the object. This parameter is used to specify the initial values of the and properties.
  A value that determines how the file will be shared by processes.
  A containing the desired buffer size in bytes.
  A value that specifies whether to use asynchronous I/O or synchronous I/O. If the underlying operating system does not support asynchronous I/O, the ignores this parameter and uses synchronous I/O.

  Constructs and initializes a new instance of the class.

  This constructor sets read/write access to the file.

  is not required to be a file stored on disk; it can be any part of a system that supports access via streams. For example, depending on the system, this class might be able to access a physical device.

  is for all objects that encapsulate files. If indicates a device that does not support seeking, the property on the resulting is required to be . For additional information, see .

  is .
  is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.

  is less than or equal to zero.

  -or-

  , , or contain an invalid value.

  is or , but the specified file cannot be found. If a different mode is specified and the file cannot be found, a new one is created.

  An I/O error occurred, such as specifying and the file specified by already exists.
  The caller does not have the required permission.
  The directory information specified by does not exist.
  The requested is not permitted by the operating system for the specified .
  The length of or the absolute path information for exceeds the system-defined maximum length.
  Requires permission to read, write, and append to files. See , , and .
  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  2.0.0.0
  4.0.0.0
- Constructor
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  To be added.
  2.0.0.0
  4.0.0.0
- Method
  System.IAsyncResult `` `` `` `` `` A array that specifies the buffer to read data into. A containing the zero based byte offset in at which to begin writing data read from the stream. A containing the maximum number of bytes to read. A delegate that references the method to be called when the asynchronous read operation is completed. An application-defined object containing the status of the asynchronous read.

  Begins an asynchronous read.

  A that references the asynchronous read.

  To determine the number of bytes read, call with the returned .

  Multiple simultaneous asynchronous requests render the request completion order uncertain.

  Use the property to determine whether the current instance supports reading. For additional information, see .

  This method overrides .

  The sum of is greater than the length of . is . or is negative. The asynchronous read operation attempted to read past the end of the file. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.IAsyncResult `` `` `` `` `` A array buffer containing data to write to the current stream. A containing the zero-based byte offset in , which marks the beginning of the data to written to the current stream. A containing the maximum number of bytes to write. A delegate that references the method to be called when the asynchronous write operation is completed. An application-defined object containing the status of the asynchronous write.

  Begins an asynchronous write operation.

  A that references the asynchronous write.

  Multiple simultaneous asynchronous requests render the request completion order uncertain.

  Use the property to determine whether the current instance supports writing. For additional information, see .

  This method overrides .

  The sum of is greater than the length of . is . or is negative. An I/O error occurred. The stream does not support writing. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Property
  System.Boolean

  Gets a value indicating whether the current stream supports reading.

  if the stream supports reading; if the stream is closed or was opened with write-only access.

  This property is read-only.

  This property overrides .

  If a class derived from does not support reading, the method throws a .

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Property
  System.Boolean

  Gets a value indicating whether the current stream supports seeking.

  if the stream supports seeking; if the stream is closed or if the was constructed from an operating-system handle such as a pipe or output to the console.

  If a class derived from does not support seeking, a call to (both and ), , or throws a .

  This property overrides .

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Property
  System.Boolean

  Gets a value indicating whether the current stream supports writing.

  if the stream supports writing; if the stream is closed or was opened with read-only access.

  If a class derived from does not support writing, a call to or will throw a .

  This property overrides .

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Void

  Closes the file and releases any resources associated with the current file stream.

  This method is equivalent to ().

  Any data previously written to the buffer is copied to the file before the file stream is closed, so it is not necessary to call before invoking . Following a call to , any operations on the file stream might raise exceptions. Invoking this method on the same instance multiple times does not result in an exception.

  The method invokes so that the file stream is closed before the garbage collector finalizes the object. However, objects writing to the , such as a , might not have flushed the data from their internal buffers to the when the call to closes the stream. To prevent data loss, always call on the highest-level object.

  This method overrides .

  0
  1.0.5000.0
- Method
  System.Void `` Specify to release both managed and unmanaged resources, or specify to release only unmanaged resources.

  Releases the unmanaged resources used by the and optionally releases the managed resources.

  When the parameter is , this method releases all resources held by any managed objects that this references.

  can be called multiple times by other objects. When overriding (), be careful not to reference objects that have been previously disposed in an earlier call to .

  An I/O error occurred. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Int32 `` The object for the pending asynchronous request.

  Ends a pending asynchronous read request, and blocks until the read request has completed.

  A containing the number of bytes read from the stream. Returns 0 only if the end of the file has been reached, otherwise, this method blocks until at least one byte is available.

  will block until the I/O operation has completed.

  This method overrides .

  is . was not returned by a call to . was called multiple times with . 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Void `` The object for the pending asynchronous request.

  Ends an asynchronous write, blocking until the I/O operation has completed.

  will block until the I/O operation has completed.

  This method overrides .

  is . was not returned by a call to . was called multiple times with . 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Void

  Releases the resources held by the current instance.

  closes the .

  Application code does not call this method; it is automatically invoked by during garbage collection unless finalization by the garbage collector has been disabled. For more information, see , and .

  This method overrides .

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Method
  System.Void

  Updates the underlying file with the current state of the buffer and subsequently clears the buffer.

  A buffer can be used either for reading or writing. If data was copied to the buffer for writing, it is written to the file and the buffer is cleared.

  If data was copied to the buffer for reading, and the property is , the current position within the file is decremented by the number of unread bytes in the buffer. The buffer is then cleared.

  This method overrides .

  An I/O error occurred. The current instance has already been closed. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  4.0.0.0
  System.Void `` To be added. To be added. To be added.
- Method
  System.Security.AccessControl.FileSecurity To be added. To be added. To be added. 2.0.0.0 4.0.0.0
- Property
  System.IntPtr To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0 System.Obsolete("Use SafeFileHandle instead")
- Property
  System.Boolean

  Gets a value indicating whether the current instance was opened asynchronously or synchronously.

  if the current was opened asynchronously; otherwise, .

  This property is read-only.

  0
  1.0.5000.0
  2.0.0.0
  4.0.0.0
- Property
  System.Int64

  Gets the length in bytes of the stream.

  A value containing the length of the stream in bytes.

  This property is read-only.

  for this stream is . An I/O error occurred, such as the file being closed. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Void `` `` To be added. To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0
- Property
  System.String To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0
- Property
  System.Int64

  Gets or sets the current position of this stream.

  A containing the current position of this stream.

  The position can be set beyond the end of the stream.

  The current stream does not support seeking. An I/O error occurred. Attempted seeking past the end of a stream that does not support this. The value specified for a set operation is negative. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Int32 `` `` `` A array. When this method returns, the bytes between and in are replaced by the bytes read from the current stream. A containing the byte offset in at which to begin writing data read from the current stream. A containing maximum number of bytes to read.

  Reads a block of bytes from the stream and returns the data in the specified buffer.

  A containing the total number of bytes read into the buffer, or zero if the end of the stream is reached.

  The method returns zero only after reaching the end of the stream. Otherwise, always reads at least one byte from the stream before returning. If no data is available from the stream, this method blocks until at least one byte of data can be returned.

  If the read operation is successful, the current position of the stream is advanced by the number of bytes read. If an exception occurs, the current position of the stream is unchanged.

  Use the property to determine whether the current instance supports reading. For additional information, see .

  This method overrides .

  is . or is negative. The current stream does not support reading. An I/O error occurred. + is greater than the length of . The current stream is closed. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Int32

  Reads a byte from the file and advances the read position one byte.

  The byte cast to a , or -1 if the end of the stream has been reached.

  Use the property to determine whether the current instance supports reading. For additional information, see .

  This method overrides .

  The current stream is closed. The current stream does not support reading. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Property
  Microsoft.Win32.SafeHandles.SafeFileHandle To be added. To be added. To be added. 2.0.0.0 4.0.0.0
- Method
  System.Int64 `` `` A containing the position relative to from which to begin seeking. A value specifying the beginning, the end, or the current position as a reference point for

  Changes the position within the current stream by the given offset, which is relative to the stated origin.

  A containing the new position in the stream.

  Use the property to determine whether the current instance supports seeking. For additional information, see .

  The position can be set beyond the end of the stream.

  This method overrides .

  An I/O error occurred. The stream does not support seeking. Attempted seeking before the beginning of the stream or to more than one byte past the end of the stream. The current stream is closed. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Void `` To be added. To be added. To be added. 2.0.0.0 4.0.0.0
- Method
  System.Void `` A that specifies the new length of the stream.

  Sets the length of the current stream to the specified value.

  If is less than the current length of the stream, the stream is truncated. If is greater than the current length of the stream, the stream is expanded, and the contents of the stream between the old and the new length are undefined. A stream is required to support both writing and seeking to implement .

  Use the property to determine whether the current instance supports writing, and the property to determine whether seeking is supported. For additional information, see and .

  This method overrides .

  An I/O error occurred. The current stream does not support writing and seeking. is less than zero. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Void `` `` To be added. To be added. To be added. To be added. 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Void `` `` `` The array to read. A that specifies the byte offset in at which to begin reading. A that specifies the maximum number of bytes to write to the current stream.

  Writes a block of bytes from a specified byte array to the current stream.

  If the write operation is successful, the current position of the stream is advanced by the number of bytes written. If an exception occurs, the current position of the stream is unchanged.

  Use the property to determine whether the current instance supports writing. For additional information, see .

  This method overrides .

  is . + is greater than the length of . or is negative. An I/O error occurred. The current stream does not support writing. An I/O error occurred. 0 1.0.5000.0 2.0.0.0 4.0.0.0
- Method
  System.Void `` A to write to the stream.

  Writes a byte to the current position in the file stream.

  Use method to write a byte to a efficiently.

  Use the property to determine whether the current instance supports writing. For additional information, see .

  This method overrides .

  The current stream is closed. The current stream does not support writing. The current stream is closed. 0 1.0.5000.0 2.0.0.0 4.0.0.0
0 System.Runtime.InteropServices.ComVisible(true)