IsolatedStorageFileStream Class
Exposes a file within isolated storage.
System.MarshalByRefObject
System.IO.Stream
System.IO.FileStream
System.IO.IsolatedStorage.IsolatedStorageFileStream
Assembly: mscorlib (in mscorlib.dll)
The IsolatedStorageFileStream type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | IsolatedStorageFileStream(String, FileMode) | Initializes a new instance of an IsolatedStorageFileStream object giving access to the file designated by path in the specified mode. |
![]() | IsolatedStorageFileStream(String, FileMode, FileAccess) | Initializes a new instance of the IsolatedStorageFileStream class giving access to the file designated by path, in the specified mode, with the kind of access requested. |
![]() | IsolatedStorageFileStream(String, FileMode, IsolatedStorageFile) | Initializes a new instance of the IsolatedStorageFileStream class giving access to the file designated by path, in the specified mode, and in the context of the IsolatedStorageFile specified by isf. |
![]() | IsolatedStorageFileStream(String, FileMode, FileAccess, FileShare) | Initializes a new instance of the IsolatedStorageFileStream class giving access to the file designated by path, in the specified mode, with the specified file access, using the file sharing mode specified by share. |
![]() | IsolatedStorageFileStream(String, FileMode, FileAccess, IsolatedStorageFile) | Initializes a new instance of the IsolatedStorageFileStream class giving access to the file designated by path in the specified mode, with the specified file access, and in the context of the IsolatedStorageFile specified by isf. |
![]() | IsolatedStorageFileStream(String, FileMode, FileAccess, FileShare, Int32) | Initializes a new instance of the IsolatedStorageFileStream class giving access to the file designated by path, in the specified mode, with the specified file access, using the file sharing mode specified by share, with the buffersize specified. |
![]() | IsolatedStorageFileStream(String, FileMode, FileAccess, FileShare, IsolatedStorageFile) | Initializes a new instance of the IsolatedStorageFileStream class giving access to the file designated by path, in the specified mode, with the specified file access, using the file sharing mode specified by share, and in the context of the IsolatedStorageFile specified by isf. |
![]() | IsolatedStorageFileStream(String, FileMode, FileAccess, FileShare, Int32, IsolatedStorageFile) | Initializes a new instance of the IsolatedStorageFileStream class giving access to the file designated by path, in the specified mode, with the specified file access, using the file sharing mode specified by share, with the buffersize specified, and in the context of the IsolatedStorageFile specified by isf. |
| Name | Description | |
|---|---|---|
![]() | CanRead | Gets a Boolean value indicating whether the file can be read. (Overrides FileStream.CanRead.) |
![]() | CanSeek | Gets a Boolean value indicating whether seek operations are supported. (Overrides FileStream.CanSeek.) |
![]() | CanTimeout | Gets a value that determines whether the current stream can time out. (Inherited from Stream.) |
![]() | CanWrite | Gets a Boolean value indicating whether you can write to the file. (Overrides FileStream.CanWrite.) |
![]() | Handle | Obsolete. Gets the file handle for the file that the current IsolatedStorageFileStream object encapsulates. Accessing this property is not permitted on an IsolatedStorageFileStream object, and throws an IsolatedStorageException. (Overrides FileStream.Handle.) |
![]() | IsAsync | Gets a Boolean value indicating whether the IsolatedStorageFileStream object was opened asynchronously or synchronously. (Overrides FileStream.IsAsync.) |
![]() | Length | Gets the length of the IsolatedStorageFileStream object. (Overrides FileStream.Length.) |
![]() | Name | Gets the name of the FileStream that was passed to the constructor. (Inherited from FileStream.) |
![]() | Position | Gets or sets the current position of the current IsolatedStorageFileStream object. (Overrides FileStream.Position.) |
![]() | ReadTimeout | Gets or sets a value, in miliseconds, that determines how long the stream will attempt to read before timing out. (Inherited from Stream.) |
![]() | SafeFileHandle | Gets a SafeFileHandle object that represents the operating system file handle for the file that the current IsolatedStorageFileStream object encapsulates. (Overrides FileStream.SafeFileHandle.) |
![]() | WriteTimeout | Gets or sets a value, in miliseconds, that determines how long the stream will attempt to write before timing out. (Inherited from Stream.) |
| Name | Description | |
|---|---|---|
![]() | BeginRead | Begins an asynchronous read. (Overrides FileStream.BeginRead(Byte(), Int32, Int32, AsyncCallback, Object).) |
![]() | BeginWrite | Begins an asynchronous write. (Overrides FileStream.BeginWrite(Byte(), Int32, Int32, AsyncCallback, Object).) |
![]() | Close | Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. (Inherited from Stream.) |
![]() | CopyTo(Stream) | Reads the bytes from the current stream and writes them to the destination stream. (Inherited from Stream.) |
![]() | CopyTo(Stream, Int32) | Reads all the bytes from the current stream and writes them to a destination stream, using a specified buffer size. (Inherited from Stream.) |
![]() | CreateObjRef | 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 | Obsolete. Allocates a WaitHandle object. (Inherited from Stream.) |
![]() | Dispose | Releases all resources used by the Stream. (Inherited from Stream.) |
![]() | Dispose(Boolean) | Releases the unmanaged resources used by the IsolatedStorageFileStream and optionally releases the managed resources. (Overrides FileStream.Dispose(Boolean).) |
![]() | EndRead | Ends a pending asynchronous read request. (Overrides FileStream.EndRead(IAsyncResult).) |
![]() | EndWrite | Ends an asynchronous write. (Overrides FileStream.EndWrite(IAsyncResult).) |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Ensures that resources are freed and other cleanup operations are performed when the garbage collector reclaims the FileStream. (Inherited from FileStream.) |
![]() | Flush | Clears buffers for this stream and causes any buffered data to be written to the file. (Overrides FileStream.Flush.) |
![]() | 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. (Overrides FileStream.Flush(Boolean).) |
![]() | GetAccessControl | Gets a FileSecurity object that encapsulates the access control list (ACL) entries for the file described by the current FileStream object. (Inherited from FileStream.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetLifetimeService | Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | InitializeLifetimeService | Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.) |
![]() | Lock | Prevents other processes from reading from or writing to the FileStream. (Inherited from FileStream.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | MemberwiseClone(Boolean) | Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.) |
![]() | ObjectInvariant | Infrastructure. Provides support for a Contract. (Inherited from Stream.) |
![]() | Read | Copies bytes from the current buffered IsolatedStorageFileStream object to an array. (Overrides FileStream.Read(Byte(), Int32, Int32).) |
![]() | ReadByte | Reads a single byte from the IsolatedStorageFileStream object in isolated storage. (Overrides FileStream.ReadByte.) |
![]() | Seek | Sets the current position of this IsolatedStorageFileStream object to the specified value. (Overrides FileStream.Seek(Int64, SeekOrigin).) |
![]() | SetAccessControl | Applies access control list (ACL) entries described by a FileSecurity object to the file described by the current FileStream object. (Inherited from FileStream.) |
![]() | SetLength | Sets the length of this IsolatedStorageFileStream object to the specified value. (Overrides FileStream.SetLength(Int64).) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
![]() | Unlock | Allows access by other processes to all or part of a file that was previously locked. (Inherited from FileStream.) |
![]() | Write | Writes a block of bytes to the IsolatedStorageFileStream object using data read from a byte array. (Overrides FileStream.Write(Byte(), Int32, Int32).) |
![]() | WriteByte | Writes a single byte to the IsolatedStorageFileStream object. (Overrides FileStream.WriteByte(Byte).) |
Use this class to read, write and create files in isolated storage.
Since this class extends FileStream, you can use an instance of IsolatedStorageFileStream in most situations where a FileStream might otherwise be used, such as to construct a StreamReader or StreamWriter.
The following console application demonstrates how you can use IsolatedStorageFile and IsolatedStorageFileStream to write data to an Isolated Storage file. The user is requested to log in. If the user is a new user, a News URL and a Sports URL are recorded as personal preferences in Isolated Storage. If the user is a returning user, the user's current preferences are displayed. The code examples used throughout this namespace are presented in the context of this sample application. You can use the Storeadm.exe (Isolated Storage Tool) utility to list and remove the Isolated Storage files that are created with this console application.
'This sample demonstrates methods of classes found in the System.IO IsolatedStorage namespace. Imports System Imports System.IO Imports System.IO.IsolatedStorage Imports System.Security.Policy Imports Microsoft.VisualBasic Imports Microsoft.Win32.SafeHandles Imports System.Security.Permissions Namespace ISOCS _ Class ConsoleApp <STAThread()> _ Overloads Shared Sub Main(ByVal args() As String) ' Prompt the user for their username. Console.WriteLine("Enter your login ID:") ' Does no error checking. Dim lp As New LoginPrefs(Console.ReadLine()) If lp.NewPrefs Then Console.WriteLine("Please set preferences for a new user.") GatherInfoFromUser(lp) ' Write the new preferences to storage. Dim percentUsed As Double = lp.SetPrefsForUser() Console.WriteLine(("Your preferences have been written. Current space used is " & percentUsed.ToString() & " %")) Else Console.WriteLine("Welcome back.") Console.WriteLine("Your preferences have expired, please reset them.") GatherInfoFromUser(lp) lp.SetNewPrefsForUser() Console.WriteLine("Your news site has been set to {0}" & ControlChars.Cr & " and your sports site has been set to {1}.", lp.NewsUrl, lp.SportsUrl) End If lp.GetIsoStoreInfo() Console.WriteLine("Enter 'd' to delete the IsolatedStorage files and exit, or press any other key to exit without deleting files.") Dim consoleInput As String = Console.ReadLine() If consoleInput.ToLower() = "d" Then lp.DeleteFiles() lp.DeleteDirectories() End If End Sub 'Main Shared Sub GatherInfoFromUser(ByVal lp As LoginPrefs) Console.WriteLine("Please enter the URL of your news site.") lp.NewsUrl = Console.ReadLine() Console.WriteLine("Please enter the URL of your sports site.") lp.SportsUrl = Console.ReadLine() End Sub 'GatherInfoFromUser End Class 'ConsoleApp _ <SecurityPermissionAttribute(SecurityAction.Demand, Flags:=SecurityPermissionFlag.UnmanagedCode)> _ Public Class LoginPrefs Public Sub New(ByVal myUserName As String) userName = myUserName myNewPrefs = GetPrefsForUser() End Sub 'New Private userName As String Private myNewsUrl As String Public Property NewsUrl() As String Get Return myNewsUrl End Get Set(ByVal Value As String) myNewsUrl = Value End Set End Property Private mySportsUrl As String Public Property SportsUrl() As String Get Return mySportsUrl End Get Set(ByVal Value As String) mySportsUrl = Value End Set End Property Private myNewPrefs As Boolean Public ReadOnly Property NewPrefs() As Boolean Get Return myNewPrefs End Get End Property Private Function GetPrefsForUser() As Boolean Try ' Retrieve an IsolatedStorageFile for the current Domain and Assembly. Dim isoFile As IsolatedStorageFile = _ IsolatedStorageFile.GetStore(IsolatedStorageScope.User _ Or IsolatedStorageScope.Assembly _ Or IsolatedStorageScope.Domain, Nothing, Nothing) Dim isoStream As New IsolatedStorageFileStream(Me.userName, FileMode.Open, _ FileAccess.Read, FileShare.Read) ' farThe code executes to this point only if a file corresponding to the username exists. ' Though you can perform operations on the stream, you cannot get a handle to the file. Try Dim aFileHandle As SafeFileHandle = isoStream.SafeFileHandle Console.WriteLine(("A pointer to a file handle has been obtained. " & aFileHandle.ToString() & " " & aFileHandle.GetHashCode())) Catch ex As Exception ' Handle the exception. Console.WriteLine("Expected exception") Console.WriteLine(ex.ToString()) End Try Dim reader As New StreamReader(isoStream) ' Read the data. Me.NewsUrl = reader.ReadLine() Me.SportsUrl = reader.ReadLine() reader.Close() isoFile.Close() Return False Catch ex As System.IO.FileNotFoundException ' Expected exception if a file cannot be found. This indicates that we have a new user. Return True End Try End Function 'GetPrefsForUser Public Function GetIsoStoreInfo() As Boolean Try 'Get a User store with type evidence for the current Domain and the Assembly. Dim isoFile As IsolatedStorageFile = _ IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or _ IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, _ GetType(System.Security.Policy.Url), GetType(System.Security.Policy.Url)) Dim dirNames As String() = isoFile.GetDirectoryNames("*") Dim fileNames As String() = isoFile.GetFileNames("*") Dim name As String ' List directories currently in this Isolated Storage. If dirNames.Length > 0 Then For Each name In dirNames Console.WriteLine("Directory Name: " & name) Next name End If ' List the files currently in this Isolated Storage. ' The list represents all users who have personal preferences stored for this application. If fileNames.Length > 0 Then For Each name In fileNames Console.WriteLine("File Name: " & name) Next name End If isoFile.Close() Return True Catch ex As Exception Console.WriteLine(ex.ToString()) End Try End Function 'GetIsoStoreInfo Public Function SetPrefsForUser() As Double Try Dim isoFile As IsolatedStorageFile isoFile = IsolatedStorageFile.GetUserStoreForDomain() ' Open or create a writable file. Dim isoStream As New IsolatedStorageFileStream(Me.userName, FileMode.OpenOrCreate, _ FileAccess.Write, isoFile) Dim writer As New StreamWriter(isoStream) writer.WriteLine(Me.NewsUrl) writer.WriteLine(Me.SportsUrl) ' Calculate the amount of space used to record the user's preferences. Dim d As Double = Convert.ToDouble(isoFile.CurrentSize) / Convert.ToDouble(isoFile.MaximumSize) Console.WriteLine(("CurrentSize = " & isoFile.CurrentSize.ToString())) Console.WriteLine(("MaximumSize = " & isoFile.MaximumSize.ToString())) ' StreamWriter.Close implicitly closes isoStream. writer.Close() isoFile.Dispose() isoFile.Close() Return d Catch ex As Exception ' Add code here to handle the exception. Console.WriteLine(ex) Return 0.0 End Try End Function 'SetPrefsForUser Public Sub DeleteFiles() Try Dim isoFile As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, GetType(System.Security.Policy.Url), GetType(System.Security.Policy.Url)) Dim name As String Dim dirNames As String() = isoFile.GetDirectoryNames("*") Dim fileNames As String() = isoFile.GetFileNames("*") ' List the files currently in this Isolated Storage. ' The list represents all users who have personal ' preferences stored for this application. If fileNames.Length > 0 Then For Each name In fileNames ' Delete the files. isoFile.DeleteFile(name) Next name 'Confirm no files are left. fileNames = isoFile.GetFileNames("*") End If Catch ex As Exception Console.WriteLine(ex.ToString()) End Try End Sub 'DeleteFiles ' This method deletes directories in the specified Isolated Storage, after first ' deleting the files they contain. In this example, the Archive directory is deleted. ' There should be no other directories in this Isolated Storage. Public Sub DeleteDirectories() Try Dim isoFile As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User _ Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, _ GetType(System.Security.Policy.Url), GetType(System.Security.Policy.Url)) Dim name As String Dim dirNames As String() = isoFile.GetDirectoryNames("*") Dim fileNames As String() = isoFile.GetFileNames("Archive\*") ' Delete all the files currently in the Archive directory. If fileNames.Length > 0 Then For Each name In fileNames isoFile.DeleteFile(("Archive\" & name)) Next name 'Confirm no files are left. fileNames = isoFile.GetFileNames("Archive\*") End If If dirNames.Length > 0 Then For Each name In dirNames ' Delete the Archive directory. isoFile.DeleteDirectory(name) Next name End If dirNames = isoFile.GetDirectoryNames("*") isoFile.Remove() Catch ex As Exception Console.WriteLine(ex.ToString()) End Try End Sub 'DeleteDirectories Public Function SetNewPrefsForUser() As Double Try Dim inputChar As Byte Dim isoFile As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, GetType(System.Security.Policy.Url), GetType(System.Security.Policy.Url)) ' If this is not a new user, archive the old preferences and ' overwrite them using the new preferences. If Not Me.myNewPrefs Then If isoFile.GetDirectoryNames("Archive").Length = 0 Then isoFile.CreateDirectory("Archive") Else Dim source As New IsolatedStorageFileStream(Me.userName, FileMode.OpenOrCreate, isoFile) Dim canWrite, canRead As Boolean ' This is the stream from which data will be read. If source.CanRead Then canRead = True Else canRead = False Console.WriteLine("Is the source file readable? " & canRead) Console.WriteLine("Creating new IsolatedStorageFileStream for Archive.") ' Open or create a writable file. Dim target As New IsolatedStorageFileStream("Archive\ " & Me.userName, _ FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, isoFile) ' This is the stream to which data will be written. If target.CanWrite Then canWrite = True Else canWrite = False Console.WriteLine("Is the target file writable? " & canWrite) target.SetLength(0) 'rewind the target file ' Stream the old file to a new file in the Archive directory. If source.IsAsync And target.IsAsync Then ' IsolatedStorageFileStreams cannot be asynchronous. However, you ' can use the asynchronous BeginRead and BeginWrite functions ' with some possible performance penalty. Console.WriteLine("IsolatedStorageFileStreams cannot be asynchronous.") Else Console.WriteLine("Writing data to the new file.") While source.Position < source.Length inputChar = CByte(source.ReadByte()) target.WriteByte(inputChar) End While ' Determine the size of the IsolatedStorageFileStream ' by checking its Length property. Console.WriteLine(("Total Bytes Read: " & source.Length)) End If ' After you have read and written to the streams, close them. target.Close() source.Close() End If End If ' Open or create a writable file with a maximum size of 10K. Dim isoStream As New IsolatedStorageFileStream(Me.userName, FileMode.OpenOrCreate, _ FileAccess.Write, FileShare.Write, 10240, isoFile) isoStream.SetLength(0) 'Position to overwrite the old data. Dim writer As New StreamWriter(isoStream) ' Update the data based on the new inputs. writer.WriteLine(Me.NewsUrl) writer.WriteLine(Me.SportsUrl) ' Calculate the amount of space used to record this user's preferences. Dim d As Double = Convert.ToDouble(isoFile.CurrentSize) / Convert.ToDouble(isoFile.MaximumSize) Console.WriteLine(("CurrentSize = " & isoFile.CurrentSize.ToString())) Console.WriteLine(("MaximumSize = " & isoFile.MaximumSize.ToString())) ' StreamWriter.Close implicitly closes isoStream. writer.Close() isoFile.Close() Return d Catch ex As Exception Console.WriteLine(ex.ToString()) Return 0.0 End Try End Function 'SetNewPrefsForUser End Class 'LoginPrefs End Namespace 'ISOCS
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.


