C# Filestream Example

On
-->

Definition

Provides a Stream for a file, supporting both synchronous and asynchronous read and write operations.

Inheritance
MarshalByRefObjectMarshalByRefObjectMarshalByRefObjectMarshalByRefObject
FileStreamFileStreamFileStreamFileStream
Attributes

Examples

The following example demonstrates some of the FileStream constructors.

The following example shows how to write to a file asynchronously. This code runs in a WPF app that has a TextBlock named UserInput and a button hooked up to a Click event handler that is named Button_Click. The file path needs to be changed to a file that exists on the computer.

Remarks

Use the FileStream class to read from, write to, open, and close files on a file system, and to manipulate other file-related operating system handles, including pipes, standard input, and standard output. You can use the Read, Write, CopyTo, and Flush methods to perform synchronous operations, or the ReadAsync, WriteAsync, CopyToAsync, and FlushAsync methods to perform asynchronous operations. Use the asynchronous methods to perform resource-intensive file operations without blocking the main thread. This performance consideration is particularly important in a Windows 8.x Store app or desktop app where a time-consuming stream operation can block the UI thread and make your app appear as if it is not working. FileStream buffers input and output for better performance.

Important

This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the 'Using an Object that Implements IDisposable' section in the IDisposable interface topic.

The IsAsync property detects whether the file handle was opened asynchronously. You specify this value when you create an instance of the FileStream class using a constructor that has an isAsync, useAsync, or options parameter. When the property is true, the stream utilizes overlapped I/O to perform file operations asynchronously. However, the IsAsync property does not have to be true to call the ReadAsync, WriteAsync, or CopyToAsync method. When the IsAsync property is false and you call the asynchronous read and write operations, the UI thread is still not blocked, but the actual I/O operation is performed synchronously.

The Seek method supports random access to files. Seek 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 members of the SeekOrigin enumeration.

Note

Disk files always support random access. At the time of construction, the CanSeek property value is set to true or false depending on the underlying file type.If the underlying file type is FILE_TYPE_DISK, as defined in winbase.h, the CanSeek property value is true. Otherwise, the CanSeek property value is false.

If a process terminates with part of a file locked or closes a file that has outstanding locks, the behavior is undefined.

For directory operations and other file operations, see the File, Directory, and Path classes. The File class is a utility class that has static methods primarily for the creation of FileStream objects based on file paths. The MemoryStream class creates a stream from a byte array and is similar to the FileStream class.

For a list of common file and directory operations, see Common I/O Tasks.

Detection of Stream Position Changes

When a FileStream object does not have an exclusive hold on its handle, another thread could access the file handle concurrently and change the position of the operating system's file pointer that is associated with the file handle. In this case, the cached position in the FileStream object and the cached data in the buffer could be compromised. The FileStream object routinely performs checks on methods that access the cached buffer to ensure that the operating system's handle position is the same as the cached position used by the FileStream object.

He is one seriously bad-assed ex-con who attempts to take down the criminal organizations that smoked his brother and father. He claims for revenge! Free download total overdose 2. Playing as Ramiro Cruz you will have to kill everything moving on the screen, and it will bring back to your mind Robert Rodriguez movies. ItÂ’s got serious game chops, slapstick humor, better-than-decent graphics, and a whole lot of crazy fun buried deep in its core.

If an unexpected change in the handle position is detected in a call to the Read method, the .NET Framework discards the contents of the buffer and reads the stream from the file again. This can affect performance, depending on the size of the file and any other processes that could affect the position of the file stream.

If an unexpected change in the handle position is detected in a call to the Write method, the contents of the buffer are discarded and an IOException exception is thrown.

A FileStream object will not have an exclusive hold on its handle when either the SafeFileHandle property is accessed to expose the handle or the FileStream object is given the SafeFileHandle property in its constructor.

Constructors

FileStream(IntPtr, FileAccess)FileStream(IntPtr, FileAccess)FileStream(IntPtr, FileAccess)FileStream(IntPtr, FileAccess)

Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission.

FileStream(IntPtr, FileAccess, Boolean)FileStream(IntPtr, FileAccess, Boolean)FileStream(IntPtr, FileAccess, Boolean)FileStream(IntPtr, FileAccess, Boolean)

Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission and FileStream instance ownership.

FileStream(IntPtr, FileAccess, Boolean, Int32)FileStream(IntPtr, FileAccess, Boolean, Int32)FileStream(IntPtr, FileAccess, Boolean, Int32)FileStream(IntPtr, FileAccess, Boolean, Int32)

Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, FileStream instance ownership, and buffer size.

FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean)FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean)FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean)FileStream(IntPtr, FileAccess, Boolean, Int32, Boolean)

Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, FileStream instance ownership, buffer size, and synchronous or asynchronous state.

FileStream(SafeFileHandle, FileAccess)FileStream(SafeFileHandle, FileAccess)FileStream(SafeFileHandle, FileAccess)FileStream(SafeFileHandle, FileAccess)

Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission.

FileStream(SafeFileHandle, FileAccess, Int32)FileStream(SafeFileHandle, FileAccess, Int32)FileStream(SafeFileHandle, FileAccess, Int32)FileStream(SafeFileHandle, FileAccess, Int32)

Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, and buffer size.

FileStream(SafeFileHandle, FileAccess, Int32, Boolean)FileStream(SafeFileHandle, FileAccess, Int32, Boolean)FileStream(SafeFileHandle, FileAccess, Int32, Boolean)FileStream(SafeFileHandle, FileAccess, Int32, Boolean)

Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, buffer size, and synchronous or asynchronous state.

FileStream(String, FileMode)FileStream(String, FileMode)FileStream(String, FileMode)FileStream(String, FileMode)

Initializes a new instance of the FileStream class with the specified path and creation mode.

FileStream(String, FileMode, FileAccess)FileStream(String, FileMode, FileAccess)FileStream(String, FileMode, FileAccess)FileStream(String, FileMode, FileAccess)

Initializes a new instance of the FileStream class with the specified path, creation mode, and read/write permission.

FileStream(String, FileMode, FileAccess, FileShare)FileStream(String, FileMode, FileAccess, FileShare)FileStream(String, FileMode, FileAccess, FileShare)FileStream(String, FileMode, FileAccess, FileShare)

Initializes a new instance of the FileStream class with the specified path, creation mode, read/write permission, and sharing permission.

FileStream(String, FileMode, FileAccess, FileShare, Int32)FileStream(String, FileMode, FileAccess, FileShare, Int32)FileStream(String, FileMode, FileAccess, FileShare, Int32)FileStream(String, FileMode, FileAccess, FileShare, Int32)

Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, and buffer size.

FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)

Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, buffer size, and synchronous or asynchronous state.

FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions)

Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, the buffer size, and additional file options.

FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions)FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions)FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions)FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions)

Initializes a new instance of the FileStream class with the specified path, creation mode, access rights and sharing permission, the buffer size, and additional file options.

FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)FileStream(String, FileMode, FileSystemRights, FileShare, Int32, FileOptions, FileSecurity)

Initializes a new instance of the FileStream class with the specified path, creation mode, access rights and sharing permission, the buffer size, additional file options, access control and audit security.

Properties

CanReadCanReadCanReadCanRead

Gets a value that indicates whether the current stream supports reading.

CanSeekCanSeekCanSeekCanSeek

Gets a value that indicates whether the current stream supports seeking.

CanTimeoutCanTimeoutCanTimeoutCanTimeout

Gets a value that determines whether the current stream can time out.

(Inherited from Stream)
CanWriteCanWriteCanWriteCanWrite

Gets a value that indicates whether the current stream supports writing.

HandleHandleHandleHandle

Gets the operating system file handle for the file that the current FileStream object encapsulates.

IsAsyncIsAsyncIsAsyncIsAsync

Gets a value that indicates whether the FileStream was opened asynchronously or synchronously.

Dec 21, 2018 - Want an Alternative to Backuptrans Android WhatsApp Transfer? Here's the best. Backuptrans android iphone whatsapp transfer crack.

LengthLengthLengthLength

Gets the length in bytes of the stream.

NameNameNameName

Gets the absolute path of the file opened in the FileStream.

PositionPositionPositionPosition

Gets or sets the current position of this stream. Detail pintu autocad.

ReadTimeoutReadTimeoutReadTimeoutReadTimeout

Gets or sets a value, in milliseconds, that determines how long the stream will attempt to read before timing out.

(Inherited from Stream)
SafeFileHandleSafeFileHandleSafeFileHandleSafeFileHandle

Gets a SafeFileHandle object that represents the operating system file handle for the file that the current FileStream object encapsulates.

WriteTimeoutWriteTimeoutWriteTimeoutWriteTimeout

Gets or sets a value, in milliseconds, that determines how long the stream will attempt to write before timing out.

(Inherited from Stream)

Methods

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

Begins an asynchronous read operation. Consider using ReadAsync(Byte[], Int32, Int32, CancellationToken) instead.

BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

Begins an asynchronous write operation. Consider using WriteAsync(Byte[], Int32, Int32, CancellationToken) instead.

Close()Close()Close()Close()

This member overrides Close(), and more complete documentation might be available in that topic.

Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream.

CopyTo(Stream)CopyTo(Stream)CopyTo(Stream)CopyTo(Stream)

Reads the bytes from the current stream and writes them to another stream.

(Inherited from Stream)
CopyTo(Stream, Int32)CopyTo(Stream, Int32)CopyTo(Stream, Int32)CopyTo(Stream, Int32)

Reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

(Inherited from Stream)
CopyToAsync(Stream)CopyToAsync(Stream)CopyToAsync(Stream)CopyToAsync(Stream)

Asynchronously reads the bytes from the current stream and writes them to another stream.

(Inherited from Stream)
CopyToAsync(Stream, CancellationToken)CopyToAsync(Stream, CancellationToken)CopyToAsync(Stream, CancellationToken)CopyToAsync(Stream, CancellationToken)

Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified cancellation token.

(Inherited from Stream)
CopyToAsync(Stream, Int32)CopyToAsync(Stream, Int32)CopyToAsync(Stream, Int32)CopyToAsync(Stream, Int32)

Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size.

(Inherited from Stream)
CopyToAsync(Stream, Int32, CancellationToken)CopyToAsync(Stream, Int32, CancellationToken)CopyToAsync(Stream, Int32, CancellationToken)CopyToAsync(Stream, Int32, CancellationToken)
CreateObjRef(Type)CreateObjRef(Type)CreateObjRef(Type)CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
CreateWaitHandle()CreateWaitHandle()CreateWaitHandle()CreateWaitHandle()

Allocates a WaitHandle object.

(Inherited from Stream)
Dispose()Dispose()Dispose()Dispose()

Releases all resources used by the Stream.

(Inherited from Stream)
Dispose(Boolean)Dispose(Boolean)Dispose(Boolean)Dispose(Boolean)

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

DisposeAsync()DisposeAsync()DisposeAsync()DisposeAsync()
EndRead(IAsyncResult)EndRead(IAsyncResult)EndRead(IAsyncResult)EndRead(IAsyncResult)

Waits for the pending asynchronous read operation to complete. (Consider using ReadAsync(Byte[], Int32, Int32, CancellationToken) instead.)

EndWrite(IAsyncResult)EndWrite(IAsyncResult)EndWrite(IAsyncResult)EndWrite(IAsyncResult)

Ends an asynchronous write operation and blocks until the I/O operation is complete. (Consider using WriteAsync(Byte[], Int32, Int32, CancellationToken) instead.)

Equals(Object)Equals(Object)Equals(Object)Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Finalize()Finalize()Finalize()Finalize()

Ensures that resources are freed and other cleanup operations are performed when the garbage collector reclaims the FileStream.

Flush()Flush()Flush()Flush()

Clears buffers for this stream and causes any buffered data to be written to the file.

Flush(Boolean)Flush(Boolean)Flush(Boolean)Flush(Boolean)

Clears buffers for this stream and causes any buffered data to be written to the file, and also clears all intermediate file buffers.

FlushAsync()FlushAsync()FlushAsync()FlushAsync()

Asynchronously clears all buffers for this stream and causes any buffered data to be written to the underlying device.

(Inherited from Stream)
FlushAsync(CancellationToken)FlushAsync(CancellationToken)FlushAsync(CancellationToken)FlushAsync(CancellationToken)

Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.

GetAccessControl()GetAccessControl()GetAccessControl()GetAccessControl()

Gets a FileSecurity object that encapsulates the access control list (ACL) entries for the file described by the current FileStream object.

GetHashCode()GetHashCode()GetHashCode()GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()GetLifetimeService()GetLifetimeService()GetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType()GetType()GetType()GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()InitializeLifetimeService()InitializeLifetimeService()InitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
Lock(Int64, Int64)Lock(Int64, Int64)Lock(Int64, Int64)Lock(Int64, Int64)

Prevents other processes from reading from or writing to the FileStream.

MemberwiseClone()MemberwiseClone()MemberwiseClone()MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)MemberwiseClone(Boolean)MemberwiseClone(Boolean)MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
ObjectInvariant()ObjectInvariant()ObjectInvariant()ObjectInvariant()

Provides support for a Contract.

(Inherited from Stream)
Read(Byte[], Int32, Int32)Read(Byte[], Int32, Int32)Read(Byte[], Int32, Int32)Read(Byte[], Int32, Int32)

Reads a block of bytes from the stream and writes the data in a given buffer.

Read(Span<Byte>)Read(Span<Byte>)Read(Span<Byte>)Read(Span<Byte>)
ReadAsync(Byte[], Int32, Int32)ReadAsync(Byte[], Int32, Int32)ReadAsync(Byte[], Int32, Int32)ReadAsync(Byte[], Int32, Int32)

Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

(Inherited from Stream)
ReadAsync(Byte[], Int32, Int32, CancellationToken)ReadAsync(Byte[], Int32, Int32, CancellationToken)ReadAsync(Byte[], Int32, Int32, CancellationToken)ReadAsync(Byte[], Int32, Int32, CancellationToken)

Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

ReadAsync(Memory<Byte>, CancellationToken)ReadAsync(Memory<Byte>, CancellationToken)ReadAsync(Memory<Byte>, CancellationToken)ReadAsync(Memory<Byte>, CancellationToken)
ReadByte()ReadByte()ReadByte()ReadByte()

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

Seek(Int64, SeekOrigin)Seek(Int64, SeekOrigin)Seek(Int64, SeekOrigin)Seek(Int64, SeekOrigin)

Sets the current position of this stream to the given value.

SetAccessControl(FileSecurity)SetAccessControl(FileSecurity)SetAccessControl(FileSecurity)SetAccessControl(FileSecurity)

Applies access control list (ACL) entries described by a FileSecurity object to the file described by the current FileStream object.

SetLength(Int64)SetLength(Int64)SetLength(Int64)SetLength(Int64)

Sets the length of this stream to the given value.

ToString()ToString()ToString()ToString()

Returns a string that represents the current object.

(Inherited from Object)
Unlock(Int64, Int64)Unlock(Int64, Int64)Unlock(Int64, Int64)Unlock(Int64, Int64)

Allows access by other processes to all or part of a file that was previously locked.

Write(Byte[], Int32, Int32)Write(Byte[], Int32, Int32)Write(Byte[], Int32, Int32)Write(Byte[], Int32, Int32)

Writes a block of bytes to the file stream.

Write(ReadOnlySpan<Byte>)Write(ReadOnlySpan<Byte>)Write(ReadOnlySpan<Byte>)Write(ReadOnlySpan<Byte>)
WriteAsync(Byte[], Int32, Int32)WriteAsync(Byte[], Int32, Int32)WriteAsync(Byte[], Int32, Int32)WriteAsync(Byte[], Int32, Int32)

Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

(Inherited from Stream)
WriteAsync(Byte[], Int32, Int32, CancellationToken)WriteAsync(Byte[], Int32, Int32, CancellationToken)WriteAsync(Byte[], Int32, Int32, CancellationToken)WriteAsync(Byte[], Int32, Int32, CancellationToken)

Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)
WriteByte(Byte)WriteByte(Byte)WriteByte(Byte)WriteByte(Byte)

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

Explicit Interface Implementations

IDisposable.Dispose()IDisposable.Dispose()IDisposable.Dispose()IDisposable.Dispose()

Releases all resources used by the Stream.

(Inherited from Stream)

Extension Methods

GetAccessControl(FileStream)GetAccessControl(FileStream)GetAccessControl(FileStream)GetAccessControl(FileStream)
SetAccessControl(FileStream, FileSecurity)SetAccessControl(FileStream, FileSecurity)SetAccessControl(FileStream, FileSecurity)SetAccessControl(FileStream, FileSecurity)
AsInputStream(Stream)AsInputStream(Stream)AsInputStream(Stream)AsInputStream(Stream)

Converts a managed stream in the .NET for Windows Store apps to an input stream in the Windows Runtime.

AsOutputStream(Stream)AsOutputStream(Stream)AsOutputStream(Stream)AsOutputStream(Stream)

Converts a managed stream in the .NET for Windows Store apps to an output stream in the Windows Runtime.

AsRandomAccessStream(Stream)AsRandomAccessStream(Stream)AsRandomAccessStream(Stream)AsRandomAccessStream(Stream)

Converts the specified stream to a random access stream.

Applies to

See also

  • FileAccessFileAccessFileAccessFileAccess
  • FileModeFileModeFileModeFileMode
  • FileShareFileShareFileShareFileShare