Using Isolated Streams
The IsolatedStorageFile object exposes a few methods to manipulate files and directories. In particular, the CreateDirectory
method lets you create a new directory in the store.
You can indicate multiple levels of directories and CreateDirectory
will ensure that the specified path exists when it returns. You read, write, and create files using a particular flavor of streamthe IsolatedStorageFileStream
class. The file name must be relative and cannot contain any drive information. Look at this example:
Dim stm As IsolatedStorageFileStream
stm = New IsolatedStorageFileStream(file, _
Dim s As String = "Hello, world!"
You use the file stream API to create a file. The file stream API is based on the programming interface of the FileStream class from which the IsolatedStorageFileStream class is derived. Once you hold an IsolatedStorageFileStream object you can use it to work with a StreamReader
or a StreamWriter
object, both of which provide an easier and more programmer-friendly set of methods. Using StreamReader and StreamWriter you can read and write to a file in a store as you would to any other file.
Dim reader As StreamReader
reader = New StreamReader(stm)
Although the isolated storage is a miniaturized version of the operating system's file system, it does not provide a rich programming interface. Isolated storage provides for the key tasks but not always through powerful and easy-to-use tools. If you want to test to make sure a given file exists before you overwrite it you must run a For-Each
statement on an array of strings. Here is how:
Dim filenames As String()
filenames = iso.GetFileNames("test.txt")
Dim file As String
For Each file In filenames
If file = "test.txt" Then
on the IsolatedStorageFile class returns an array of strings filled with all the file names found in the specified pattern. GetFileNames
takes an expression that may contain wildcard characters such as ? or *. Wildcards are permitted only in the file name portion of the expression. You cannot use wildcards to indicate a directory path. The returned array contains only the name of items foundthat's why you can't perform your search on multiple paths. Similarly, GetDirectoryNames
selects all directories in the isolated storage that match the pattern. Both methods require an instance of the Store object (they are not static methods) and don't work recursively on the subfolders of the specified folder.
are instance methods of the IsolatedStorageFile class that allow you to remove undesired storage directories and files. These Delete
methods do not accept wildcards. To remove the whole store you use the Remove
method. It is important that you remove unneeded files and directories because the disk reserved for isolated storage is not unlimited. Administrators must tune the quotas of disk storage allocated to each single store. Two read-only long propertiesMaximumSize
let developers foresee in advance whether a given call may fail due to space constraints. Notice that quotas are assigned on a per-evidence basis, which means that the same assembly can receive different quota if run from an intranet rather than from the Internet.
Isolated storage is a built-in mechanism of .NET security that let potentially unsecure applications safely access the disk and the local file system to perform simple writing operations. The idea behind isolated storage is the sandboxthe same that secures Java applications within the boundaries of the Java virtual machine. Isolated storage provides full disk access to applications but below isolated file system subtrees. Partially trusted applications can use isolated storage to persist data without the risk of performing malicious operations against sensitive parts of the operating system. Writing partially trusted applications is a delicate art that goes far beyond the functionality of isolated storage. But that is another story and probably another article!