Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. FileStream Class
DefinitionProvides a Stream for a file, supporting both synchronous and asynchronous read and write operations. In this article
InheritanceInheritanceDerivedAttributesExamplesThe 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.
RemarksUse 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 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 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 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 ChangesWhen 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. 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
Properties
Methods
Explicit Interface ImplementationsExtension Methods
Applies toSee also
What is a files read position?A file's read position marks the location of the next item that will be read from the file. Initially, the read position is set to the beginning of the file.
Where is the read position when an input file is opened?The process of opening a file is only necessary with input files. Output files are automatically opened when data is written to them. When an input file is opened, its read position is initially set to the first item in the file.
When a file is opened for append?When a file that already exists is opened in append mode, the file's existing contents are erased. If you do not handle an exception, it is ignored by the Python interpreter and the program continues to execute. You can have more than one except clause in a try/except statement.
When a piece of data is read from a file?When a piece of data is read from a file, it is copied from the file into the program. Closing a file disconnects the communication between the file and the program. In Python, there is nothing that can be done if the program tries to access a file to read that does not exist.
|