Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.
Assembly: mscorlib (in mscorlib.dll)
Use the class to read from, write to, open, and close files on a file system, as well as to manipulate other file-related operating system handles including pipes, standard input, and standard output. You can specify read and write operations to be either synchronous or asynchronous. buffers input and output for better performance.
objects support random access to files using the Seek method. 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 properties of the SeekOrigin class.
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. Specifically, 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.
Although the synchronous methods Read and Write and the asynchronous methods BeginRead, BeginWrite, EndRead, and EndWrite can work in either synchronous or asynchronous mode, the mode affects the performance of these methods. defaults to opening files synchronously, but provides the FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) constructor to open files asynchronously.
If a process terminates with part of a file locked or closes a file that has outstanding locks, the behavior is undefined.
For directory and other file operations, see the File, Directory, and Path classes. The File class is a utility class with static methods primarily for the creation of objects based on file paths and the standard input, standard output, and standard error devices. The MemoryStream class creates a stream from a byte array and functions similarly to a .
For a list of common I/O tasks, see Common I/O Tasks.
Detection of stream position changes
When a 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 object and the cached data in the buffer could be compromised. The object routinely performs checks on methods that access the cached buffer to assure that the operating system's handle position is the same as the cached position used by the 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.
The following example demonstrates some of the constructors.
Imports System Imports System.IO Imports System.Text Public Class Test Public Shared Sub Main() Dim path As String = "c:\temp\MyTest.txt" ' Delete the file if it exists. If File.Exists(path) Then File.Delete(path) End If 'Create the file. Dim fs As FileStream = File.Create(path) AddText(fs, "This is some text") AddText(fs, "This is some more text,") AddText(fs, Environment.NewLine & "and this is on a new line") AddText(fs, Environment.NewLine & Environment.NewLine) AddText(fs, "The following is a subset of characters:" & Environment.NewLine) Dim i As Integer For i = 1 To 120 AddText(fs, Convert.ToChar(i).ToString()) Next fs.Close() 'Open the stream and read it back. fs = File.OpenRead(path) Dim b(1024) As Byte Dim temp As UTF8Encoding = New UTF8Encoding(True) Do While fs.Read(b, 0, b.Length) > 0 Console.WriteLine(temp.GetString(b)) Loop fs.Close() End Sub Private Shared Sub AddText(ByVal fs As FileStream, ByVal value As String) Dim info As Byte() = New UTF8Encoding(True).GetBytes(value) fs.Write(info, 0, info.Length) End Sub End Class
The following example opens a file or creates it if it does not already exist, and appends information to the end of the file.
Imports System Imports System.IO Imports System.Text Class FSOpenWrite Public Shared Sub Main() Dim fs As New FileStream("c:\Variables.txt", FileMode.Append, FileAccess.Write, FileShare.Write) fs.Close() Dim sw As New StreamWriter("c:\Variables.txt", True, Encoding.ASCII) Dim NextLine As String = "This is the appended text." sw.Write(NextLine) sw.Close() End Sub 'Main End Class 'FSOpenWrite
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.