public class FileStream : Stream
Object
MarshalByRefObject
Stream
FileStreamThis type implements IDisposable.
mscorlib
BCL
Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.
FileStream 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. FileStream buffers input and output for better performance.The FileStream class can open a file in one of two modes, either synchronously or asynchronously, with significant performance consequences for the synchronous methods (System.IO.FileStream.Read(System.Byte[],System.Int32,System.Int32) and System.IO.FileStream.Write(System.Byte[],System.Int32,System.Int32)) and the asynchronous methods (System.IO.FileStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) and System.IO.FileStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object) ). Both sets of methods will work in either mode; however, the mode will affect the performance of these methods. FileStream 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. [Note: 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.
FileStream objects support random access to files using the System.IO.FileStream.Seek(System.Int64,System.IO.SeekOrigin) method, and the System.IO.Stream.CanSeek properties of FileStream instances encapsulating files are set to
true
. The System.IO.FileStream.Seek(System.Int64,System.IO.SeekOrigin) 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 SeekOrigin enumeration.If a FileStream encapsulates a device that does not support seeking, its System.IO.FileStream.CanSeek property is
false
. [Note: For additional information, see System.IO.Stream.CanSeek.]
[Note: The File class provides methods for the creation of FileStream objects based on file paths. The MemoryStream class creates a stream from a byte array and functions similarly to a FileStream.]
The following example demonstrates the use of a FileStream 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
-------------------------------
System.IO Namespace
FileStream Constructors
FileStream(System.String, System.IO.FileMode) Constructor
FileStream(System.String, System.IO.FileMode, System.IO.FileAccess) Constructor
FileStream(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare) Constructor
FileStream(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, int) Constructor
FileStream(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, int, bool) Constructor
FileStream Methods
FileStream.BeginRead Method
FileStream.BeginWrite Method
FileStream.Close Method
FileStream.Dispose Method
FileStream.EndRead Method
FileStream.EndWrite Method
FileStream.Finalize Method
FileStream.Flush Method
FileStream.Read Method
FileStream.ReadByte Method
FileStream.Seek Method
FileStream.SetLength Method
FileStream.Write Method
FileStream.WriteByte Method
FileStream Properties
FileStream.CanRead Property
FileStream.CanSeek Property
FileStream.CanWrite Property
FileStream.IsAsync Property
FileStream.Length Property
FileStream.Position Property
public FileStream(string path, FileMode mode);
Constructs and initializes a new instance of the FileStream class with the specified path and creation mode.
- path
- A String containing the relative or absolute path for the file that the current FileStream object will encapsulate.
- mode
- A FileMode value that determines how to open or create the file.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. ArgumentNullException path is null
.SecurityException The caller does not have the required permission. FileNotFoundException mode is System.IO.FileMode.Truncate or System.IO.FileMode.Open, but the specified file cannot be found. If a different mode is specified and the file cannot be found, a new one is created.
IOException An I/O error occurred, such as specifying System.IO.FileMode.CreateNew when the file specified by path already exists. DirectoryNotFoundException The directory information specified in path does not exist. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. ArgumentOutOfRangeException mode contains an invalid value.
This constructor sets System.IO.FileAccess.ReadWrite access to the file, and the System.IO.Stream.CanRead and System.IO.Stream.CanWrite properties of the current instance are set totrue
.[Note: path 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 may be able to access a physical device.]
System.IO.Stream.CanSeek is
true
for all FileStream objects that encapsulate files. If path specifies a device that does not support seeking, the System.IO.FileStream.CanSeek property of the resulting FileStream is required to befalse
. [Note: For additional information, see System.IO.Stream.CanSeek . ]
Requests to open the file for writing by the current or another thread will fail until the FileStream object has been closed. Read attempts will succeed.
System.IO.FileStream Class, System.IO Namespace
public FileStream(string path, FileMode mode, FileAccess access);
Constructs and initializes a new instance of the FileStream class with the specified path, creation mode, and access type.
- path
- A String containing the relative or absolute path for the file that the current FileStream object will encapsulate.
- mode
- A FileMode value that determines how to open or create the file.
- access
- A FileAccess value that determines how the file may be accessed by the FileStream object. This parameter is used to specify the initial values of the System.IO.FileStream.CanRead and System.IO.FileStream.CanWrite properties.
Exception Type Condition ArgumentNullException path is null
.ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. FileNotFoundException mode is System.IO.FileMode.Truncate or System.IO.FileMode.Open , but the specified file was not found. If a different mode is specified and the file was not found, a new one is created.
IOException An I/O error occurred, such as specifying System.IO.FileMode.CreateNew when the file specified by path already exists. SecurityException The caller does not have the required permission. DirectoryNotFoundException The directory information specified by path does not exist. UnauthorizedAccessException The access requested is not permitted by the operating system for the specified path. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. ArgumentOutOfRangeException mode or access contain an invalid value.
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 FileStream object has been closed. Read attempts will succeed.[Note: path 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 may be able to access a physical device.]
System.IO.Stream.CanSeek is
true
for all FileStream objects that encapsulate files. If path indicates a device that does not support seeking, the System.IO.FileStream.CanSeek property on the resulting FileStream is required to befalse
. For additional information, see System.IO.Stream.CanSeek .
System.IO.FileStream Class, System.IO Namespace
public FileStream(string path, FileMode mode, FileAccess access, FileShare share);
Constructs and initializes a new instance of the FileStream class with the specified path, creation mode, access type, and sharing permission.
- path
- A String containing relative or absolute path for the file that the current FileStream object will encapsulate.
- mode
- A FileMode value that determines how to open or create the file.
- access
- A FileAccess value that determines how the file may be accessed by the FileStream object. This parameter is used to specify the initial values of the System.IO.FileStream.CanRead and System.IO.FileStream.CanWrite properties. For additional information, see System.IO.Stream.CanRead and System.IO.Stream.CanWrite.
- share
- A FileShare value that determines how the file will be shared by processes.
Exception Type Condition ArgumentNullException path is null
.ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. FileNotFoundException mode is System.IO.FileMode.Truncate or System.IO.FileMode.Open , but the specified file cannot be found. If a different mode is specified and the file cannot be found, a new one is created.
IOException An I/O error occurred, such as specifying System.IO.FileMode.CreateNew and the file specified by path already exists. SecurityException The caller does not have the required permission. DirectoryNotFoundException The directory information specified by path does not exist. UnauthorizedAccessException The access requested is not permitted by the operating system for the specified path. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. ArgumentOutOfRangeException mode, access, or share contains an invalid value.
This constructor sets read/write access to the file. Requests to open the file for writing by the current or another process will fail until the FileStream object has been closed. Read attempts will succeed.[Note: path 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 may be able to access a physical device.]
System.IO.Stream.CanSeek is
true
for all FileStream objects that encapsulate files. If path indicates a device that does not support seeking, the System.IO.FileStream.CanSeek property on the resulting FileStream is required to befalse
. For additional information, see System.IO.Stream.CanSeek .
System.IO.FileStream Class, System.IO Namespace
public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize);
Constructs and initializes a new instance of the FileStream class.
- path
- A String containing the relative or absolute path for the file that the current FileStream object will encapsulate.
- mode
- A FileMode constant that determines how to open or create the file.
- access
- A FileAccess value that determines how the file may be accessed by the FileStream object. This parameter is used to specify the initial values of the System.IO.FileStream.CanRead and System.IO.FileStream.CanWrite properties. For additional information, see System.IO.Stream.CanRead and System.IO.Stream.CanWrite .
- share
- A FileShare constant that determines how the file will be shared by processes.
- bufferSize
- A Int32 containing the desired buffer size in bytes.
Exception Type Condition ArgumentNullException The path parameter is null
.ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. ArgumentOutOfRangeException bufferSize is less than or equal to zero. -or-
mode, access, or share contain an invalid value.
FileNotFoundException mode is System.IO.FileMode.Truncate or System.IO.FileMode.Open , but the specified file cannot be found. If a different mode is specified and the file cannot be found, a new one is created.
IOException An I/O error occurred, such as specifying System.IO.FileMode.CreateNew and the file specified by path already exists. SecurityException The caller does not have the required permission. DirectoryNotFoundException The directory information specified in path does not exist. UnauthorizedAccessException The access requested is not permitted by the operating system for the specified path. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length.
[Note: path 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 may be able to access a physical device.]
System.IO.Stream.CanSeek is
true
for all FileStream objects that encapsulate files. If path indicates a device that does not support seeking, the System.IO.FileStream.CanSeek property on the resulting FileStream is required to befalse
. For additional information, see System.IO.Stream.CanSeek .
System.IO.FileStream Class, System.IO Namespace
public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync);
Constructs and initializes a new instance of the FileStream class.
- path
- A String containing the relative or absolute path for the file that the new FileStream object will encapsulate.
- mode
- A FileMode value that determines how to open or create the file.
- access
- A FileAccess value that determines how the file may be accessed by the FileStream object. This parameter is used to specify the initial values of the System.IO.FileStream.CanRead and System.IO.FileStream.CanWrite properties.
- share
- A FileShare value that determines how the file will be shared by processes.
- bufferSize
- A Int32 containing the desired buffer size in bytes.
- useAsync
- A Boolean 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 FileStream ignores this parameter and uses synchronous I/O.
Exception Type Condition ArgumentNullException path is null
.ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-defined invalid characters. ArgumentOutOfRangeException bufferSize is less than or equal to zero. -or-
mode, access, or share contain an invalid value.
FileNotFoundException mode is System.IO.FileMode.Truncate or System.IO.FileMode.Open, but the specified file cannot be found. If a different mode is specified and the file cannot be found, a new one is created.
IOException An I/O error occurred, such as specifying System.IO.FileMode.CreateNew and the file specified by path already exists. SecurityException The caller does not have the required permission. DirectoryNotFoundException The directory information specified by path does not exist. UnauthorizedAccessException The access requested is not permitted by the operating system for the specified path. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length.
This constructor sets read/write access to the file. Requests to open the file for writing by this or another process will fail until the FileStream object has been closed. Read attempts will succeed.[Note: path 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 may be able to access a physical device.]
System.IO.Stream.CanSeek is
true
for all FileStream objects that encapsulate files. If path indicates a device that does not support seeking, the System.IO.FileStream.CanSeek property on the resulting FileStream is required to befalse
. For additional information, see System.IO.Stream.CanSeek .
System.IO.FileStream Class, System.IO Namespace
public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject);
Begins an asynchronous read.
- array
- A Byte array that specifies the buffer to read data into.
- offset
- A Int32 containing the zero based byte offset in array at which to begin reading.
- numBytes
- A Int32 containing the maximum number of bytes to read.
- userCallback
- A AsyncCallback delegate that references the method to be called when the asynchronous read operation is completed.
- stateObject
- An application defined object containing the status of the asynchronous read.
A IAsyncResult that references the asynchronous read.
Exception Type Condition ArgumentException The sum of offset andnumBytes is greater than the length of array. ArgumentNullException array is null
.ArgumentOutOfRangeException offset or numBytes is negative. IOException The asynchronous read operation attempted to read past the end of the file.
To determine the number of bytes read, call System.IO.Stream.EndRead(System.IAsyncResult) with the returned IAsyncResult.Multiple simultaneous asynchronous requests render the request completion order uncertain.
[Note: Use the System.IO.FileStream.CanRead property to determine whether the current instance supports reading. For additional information, see System.IO.Stream.CanRead.
This method overrides System.IO.Stream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object).
]
System.IO.FileStream Class, System.IO Namespace
public override IAsyncResult BeginWrite(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject);
Begins an asynchronous write operation.
- array
- A Byte array buffer to write data to.
- offset
- A Int32 containing the zero based byte offset in array at which to begin writing.
- numBytes
- A Int32 containing the maximum number of bytes to write.
- userCallback
- A AsyncCallback delegate that references the method to be called when the asynchronous write operation is completed.
- stateObject
- An application defined object containing the status of the asynchronous read.
A IAsyncResult that references the asynchronous write.
Exception Type Condition ArgumentException The sum of offset and numBytes is greater than the length of array. ArgumentNullException array is null
.ArgumentOutOfRangeException offset or numBytes is negative. IOException The stream does not support writing, or an I/O error occurred.
Multiple simultaneous asynchronous requests render the request completion order uncertain.[Note: Use the System.IO.FileStream.CanWrite property to determine whether the current instance supports writing. For additional information, see System.IO.Stream.CanWrite.
This method overrides System.IO.Stream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object).
]
System.IO.FileStream Class, System.IO Namespace
public override void Close();
Closes the file and releases any resources associated with the current file stream.
This method is equivalent to System.IO.FileStream.Dispose(System.Boolean)(true
).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 System.IO.FileStream.Flush before invoking
Close
. Following a call toClose
, any operations on the file stream might raise exceptions. Invoking this method on the same instance multiple times does not result in an exception.[Usage: The System.IO.FileStream.Finalize method invokes
Close
so that the file stream is closed before the garbage collector finalizes the object. However, objects writing to the FileStream, such as a StreamWriter, might not have flushed the data from their internal buffers to the FileStream when the call toFinalize
closes the stream. To prevent data loss, always callClose
on the highest-level object.]
[Note: This method overrides System.IO.Stream.Close. ]
System.IO.FileStream Class, System.IO Namespace
protected virtual void Dispose(bool disposing);
Releases the unmanaged resources used by the FileStream and optionally releases the managed resources.
- disposing
- Specify
true
to release both managed and unmanaged resources, or specifyfalse
to release only unmanaged resources.
Exception Type Condition IOException An I/O error occurred.
When the disposing parameter istrue
, this method releases all resources held by any managed objects that this FileStream references.[Note: System.IO.FileStream.Dispose(System.Boolean) may be called multiple times by other objects. When overriding System.IO.FileStream.Dispose(System.Boolean)(Boolean), be careful not to reference objects that have been previously disposed in an earlier call to System.IO.FileStream.Dispose(System.Boolean) .
]
System.IO.FileStream Class, System.IO Namespace
public override int EndRead(IAsyncResult asyncResult);
Ends a pending asynchronous read request, and blocks until the read request has completed.
- asyncResult
- The IAsyncResult object for the pending asynchronous request.
A Int32 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.
Exception Type Condition ArgumentNullException asyncResult is null
.ArgumentException asyncResult was not returned by a call to System.IO.FileStream.BeginRead(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object). InvalidOperationException System.IO.FileStream.EndRead(System.IAsyncResult) was called multiple times with asyncResult .
EndRead
will block until the I/O operation has completed.[Note: This method overrides System.IO.Stream.EndRead(System.IAsyncResult).]
System.IO.FileStream Class, System.IO Namespace
public override void EndWrite(IAsyncResult asyncResult);
Ends an asynchronous write, blocking until the I/O operation has completed.
- asyncResult
- The IAsyncResult object for the pending asynchronous request.
Exception Type Condition ArgumentNullException asyncResult is null
.ArgumentException asyncResult was not returned by a call to System.IO.FileStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object). InvalidOperationException System.IO.FileStream.EndWrite(System.IAsyncResult) was called multiple times with asyncResult .
System.IO.FileStream.EndWrite(System.IAsyncResult) will block until the I/O operation has completed.[Note: This method overrides System.IO.Stream.EndWrite(System.IAsyncResult).]
System.IO.FileStream Class, System.IO Namespace
~FileStream();
Releases the resources held by the current instance.
System.IO.FileStream.Finalize closes the FileStream.[Note: 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 System.GC.SuppressFinalize(System.Object), and System.Object.Finalize.
This method overrides System.Object.Finalize.
]
System.IO.FileStream Class, System.IO Namespace
public override void Flush();
Updates the underlying file with the current state of the buffer and subsequently clears the buffer.
Exception Type Condition IOException An I/O error occurred. ObjectDisposedException The current instance has already been closed.
A FileStream 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 System.IO.Stream.CanSeek property is
true
, the current position within the file is decremented by the number of unread bytes in the buffer. The buffer is then cleared.[Note: This method overrides System.IO.Stream.Flush.]
System.IO.FileStream Class, System.IO Namespace
public override int Read(byte[] array, int offset, int count);
Reads a block of bytes from the stream and returns the data in the specified buffer.
- array
- A Byte array. When this method returns, the bytes between offset and (offset + count - 1) in array are replaced by the bytes read from the current stream.
- offset
- A Int32 containing the byte offset in array at which to begin writing data read from the current stream.
- count
- A Int32 containing maximum number of bytes to read.
A Int32 containing the total number of bytes read into the buffer, or zero if the end of the stream is reached.
Exception Type Condition ArgumentNullException array is null
.ArgumentOutOfRangeException offset or count is negative. NotSupportedException The current stream does not support reading. IOException An I/O error occurred. ArgumentException offset + count is greater than the length of array. ObjectDisposedException The current stream is closed.
The System.IO.FileStream.Read(System.Byte[],System.Int32,System.Int32) method returns zero only after reaching the end of the stream. Otherwise, System.IO.FileStream.Read(System.Byte[],System.Int32,System.Int32) 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.
[Note: Use the System.IO.FileStream.CanRead property to determine whether the current instance supports reading. For additional information, see System.IO.Stream.CanRead.]
[Note: This method overrides System.IO.Stream.Read(System.Byte[],System.Int32,System.Int32) .]
System.IO.FileStream Class, System.IO Namespace
public override int ReadByte();
Reads a byte from the file and advances the read position one byte.
The byte cast to a Int32, or -1 if the end of the stream has been reached.
Exception Type Condition ObjectDisposedException The current stream is closed. NotSupportedException The current stream does not support reading.
[Note: Use the System.IO.FileStream.CanRead property to determine whether the current instance supports reading. For additional information, see System.IO.Stream.CanRead.This method overrides System.IO.Stream.ReadByte.
]
System.IO.FileStream Class, System.IO Namespace
public override long Seek(long offset, SeekOrigin origin);
Sets the current position of the current stream to the specified value.
- offset
- A Int64 containing the position relative to origin from which to begin seeking.
- origin
- A SeekOrigin value specifying the beginning, the end, or the current position as a reference point for offset.
A Int64 containing the new position in the stream.
Exception Type Condition IOException An I/O error occurred. NotSupportedException The stream does not support seeking. ArgumentException Attempted seeking before the beginning of the stream or to more than one byte past the end of the stream. ObjectDisposedException The current stream is closed.
[Note: Use the System.IO.FileStream.CanSeek property to determine whether the current instance supports seeking. For additional information, see System.IO.Stream.CanSeek .]
[Usage: In order to open a new file and write to it, set the position to one byte beyond the end of the stream. This allows you to append to the file. The position cannot be set to more than one byte beyond the end of the stream.]
[Note: This method overrides System.IO.Stream.Seek(System.Int64,System.IO.SeekOrigin).]
System.IO.FileStream Class, System.IO Namespace
public override void SetLength(long value);
Sets the length of the current stream to the specified value.
- value
- A Int64 that specifies the new length of the stream.
Exception Type Condition IOException An I/O error occurred. NotSupportedException The current stream does not support writing and seeking. ArgumentOutOfRangeException value is less than zero.
If value is less than the current length of the stream, the stream is truncated. If value 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 System.IO.FileStream.SetLength(System.Int64).[Note: Use the System.IO.FileStream.CanWrite property to determine whether the current instance supports writing, and the System.IO.FileStream.CanSeek property to determine whether seeking is supported. For additional information, see System.IO.Stream.CanWrite and System.IO.Stream.CanSeek .
This method overrides System.IO.Stream.SetLength(System.Int64).
]
System.IO.FileStream Class, System.IO Namespace
public override void Write(byte[] array, int offset, int count);
Writes a block of bytes from a specified byte array to the current stream.
- array
- The Byte array to read.
- offset
- A Int32 that specifies the byte offset in array at which to begin reading.
- count
- A Int32 that specifies the maximum number of bytes to write to the current stream.
Exception Type Condition ArgumentNullException array is null
.ArgumentException offset + count is greater than the length of array. ArgumentOutOfRangeException offset or count is negative. IOException An I/O error occurred. NotSupportedException The current stream does not support writing.
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.[Note: Use the System.IO.FileStream.CanWrite property to determine whether the current instance supports writing. For additional information, see System.IO.Stream.CanWrite.
This method overrides System.IO.Stream.Write(System.Byte[],System.Int32,System.Int32).
]
System.IO.FileStream Class, System.IO Namespace
public override void WriteByte(byte value);
Writes a byte to the current position in the file stream.
- value
- A Byte to write to the stream.
Exception Type Condition IOException The current stream is closed. NotSupportedException The current stream does not support writing.
[Usage: Use System.IO.FileStream.WriteByte(System.Byte) method to write a byte to a FileStream efficiently.]
[Note: Use the System.IO.FileStream.CanWrite property to determine whether the current instance supports writing. For additional information, see System.IO.Stream.CanWrite.
This method overrides System.IO.Stream.WriteByte(System.Byte).
]
System.IO.FileStream Class, System.IO Namespace
public override bool CanRead { get; }
Gets a Boolean value indicating whether the current stream supports reading.
true
if the stream supports reading;false
if the stream is closed or was opened with write-only access.
This property is read-only.[Note: This property overrides System.IO.Stream.CanRead.
If a class derived from Stream does not support reading, the
Read
andPeek
methods throw a NotSupportedException .]
System.IO.FileStream Class, System.IO Namespace
public override bool CanSeek { get; }
Gets a Boolean value indicating whether the current stream supports seeking.
true
if the stream supports seeking;false
if the stream is closed or if the FileStream was constructed from an operating-system handle such as a pipe or output to the console.
[Note: If a class derived from Stream does not support seeking, a call to System.IO.FileStream.Length (bothget
andset
), System.IO.FileStream.Position, or System.IO.FileStream.Seek(System.Int64,System.IO.SeekOrigin) throws a NotSupportedException .This property overrides System.IO.Stream.CanSeek.
]
System.IO.FileStream Class, System.IO Namespace
public override bool CanWrite { get; }
Gets a Boolean value indicating whether the current stream supports writing.
true
if the stream supports writing;false
if the stream is closed or was opened with read-only access.
If a class derived from Stream does not support writing, a call to System.IO.FileStream.Write(System.Byte[],System.Int32,System.Int32), System.IO.FileStream.BeginWrite(System.Byte[],System.Int32,System.Int32,System.AsyncCallback,System.Object), or System.IO.FileStream.EndWrite(System.IAsyncResult) will throw a NotSupportedException .[Note: This property overrides System.IO.Stream.CanWrite.]
System.IO.FileStream Class, System.IO Namespace
public virtual bool IsAsync { get; }
Gets a Boolean value indicating whether the current instance was opened asynchronously or synchronously.
true
if the current FileStream was opened asynchronously; otherwise,false
.
[Behaviors: This property is read-only.]
System.IO.FileStream Class, System.IO Namespace
public override long Length { get; }
Gets the length in bytes of the stream.
A Int64 value containing the length of the stream in bytes.
Exception Type Condition NotSupportedException System.IO.FileStream.CanSeek for this stream is false
.IOException An I/O error occurred, such as the file being closed.
This property is read-only.
System.IO.FileStream Class, System.IO Namespace
public override long Position { get; set; }
Gets or sets the current position of this stream.
A Int64 containing the current position of this stream.
Exception Type Condition NotSupportedException The current stream does not support seeking. IOException An I/O error occurred. EndOfStreamException Attempted seeking past the end of a stream that does not support this. ArgumentOutOfRangeException The value specified for a set operation is negative.
In order to open a new file and write to it, set the position to one byte beyond the end of the stream. This allows you to append to the file. The position cannot be set to more than one byte beyond the end of the stream.
System.IO.FileStream Class, System.IO Namespace