Isolated Storage Basicsby Mike Gunderloy
It's not unusual for an application to need to store some data for later use; maybe your application allows the user to set persistent options or save work in progress. But even the simple act of saving data can be fraught with dangers, in today's world. Consider these potential stumbling blocks:
- Your code might lack permission to write to an arbitrary file. For example, the user might be running your code in the Internet security zone.
- The user may lack permission to write data to an arbitrary file. They might be running Windows in a locked-down environment or under a roaming user profile. In these cases, the network administrator may have placed severe limitations on the user's rights to write files.
In the past, developers have dealt with these issues using a variety of types of special-purpose code that attempts to create unique disk files or registry keys. When you add the differing disk layouts of different operating systems, user-level security, and roaming user profiles into the mix, this becomes an extremely difficult task. Fortunately, this is an area where the .NET Framework offers a new and better way to handle things. This new and better way is called isolated storage.
Overview of Isolated Storage
Isolated storage assigns each user of the system an abstract location called a data compartment. Within the data compartment, there can be one or more stores. Stores can be isolated from one another by both user and assembly. That is, if two users run the same assembly, and that assembly uses isolated storage, then the two stores created are distinct from one another. Similarly, if the same user runs two different assemblies, and the assemblies use isolated storage, then the two stores created are distinct from one another.
A store acts as a complete virtual file system. You can create and delete directories and files within a store, and read and write data to the files in the store. As a practical matter, the data from a store is kept in hidden files in the file system, as shown in Figure 1, but you don't need to worry about that when using the isolated storage interfaces. The .NET Framework takes care of the details of finding a file system location that the user has permission to use.
|Figure 1. The .NET Framework uses obscure folder names for isolated storage.|
Mechanics of Isolated Storage
If you're familiar with the streams and stores model used by most I/O operations under the .NET Framework, you should find isolated storage code relatively easy to understand. I'll show you the code for some basic isolated-storage operations, followed by a more application-oriented example.
Obtaining a Store
To work with isolated storage, you'll first need to include the appropriate namespace declarations in your code:
Imports System.IO Imports System.IO.IsolatedStorage
Isolated storage stores are represented by the
IsolatedStorageFile object. There's a static method of this object
that you can use to obtain the store for the current assembly:
' Get the isolated store for this assembly Dim isf As IsolatedStorageFile = _ IsolatedStorageFile.GetUserStoreForAssembly()
Creating a Directory
IsolatedStorageFile object implements a
CreateDirectory method that you can use to create directories
within the store:
' Create a directory at the top level of the store isf.CreateDirectory("Dir1")
You can create subdirectories by specifying the entire path to the subdirectory in the call. The parent directory will be created by the same call, if necessary. Note that URL-style forward slashes are used to separate directories and subdirectories:
' Create a subdirectory isf.CreateDirectory("Dir1/Dir2")
Creating a File
Files inside of an isolated storage store are represented by an
IsolatedStorageFileStream object. To create or open a file, you can
call the constructor for this class with appropriate parameters. You can create
a new file at any level in the directory hierarchy, but the directories must
' Create a file at the top level of the store Dim isfs1 As IsolatedStorageFileStream = _ New IsolatedStorageFileStream("Rootfile.txt", _ FileMode.OpenOrCreate, FileAccess.Write, isf)
There are several overloaded constructors for the
IsolatedStorageFileStream class, but you're most likely to use the
one shown here. It takes four parameters:
- The name of the file to open or create.
FileModeconstant indicating the desired action. This can be
Create(which overwrites any existing file),
CreateNew(which throws an exception if the file exists),
Truncate(open an existing file and set its size to zero).
FileAccessconstant indicating the desired access. This can be
IsolatedStorageFileobject that represents the store where this file will be located.
Writing to a File
After you've created an
IsolatedStorageFileStream to represent a
file, writing to the file is exactly like writing to any other stream. At this
point, you can use any of the classes in the
(or elsewhere) that write to streams. For example, you can use a
StreamWriter class to write text directly into the file:
' Create or open a file at the top level of the store Dim isfs1 As IsolatedStorageFileStream = _ New IsolatedStorageFileStream("Rootfile.txt", _ FileMode.OpenOrCreate, FileAccess.Write, isf) ' Treat it like any other stream Dim sw As StreamWriter = New StreamWriter(isfs1) sw.WriteLine("Isolated storage is keen.") sw.WriteLine("You can treat it like a file.") sw.Flush() sw.Close()
Reading from a File
Reading from an isolated storage file is similar to reading from any other
stream. For example, you can use a
StreamReader object to extract
information from the file, either line by line, or character by character:
' Open a file at the top level of the store Dim isfs1 As IsolatedStorageFileStream = _ New IsolatedStorageFileStream("Rootfile.txt", _ FileMode.Open, FileAccess.Read, isf) ' Treat it like any other stream Dim sr As StreamReader = New StreamReader(isfs1) Dim sw As StringWriter = New StringWriter() While (sr.Peek() > -1) sw.WriteLine(sr.ReadLine) End While MessageBox.Show(sw.ToString, _ "Isolated Storage contents")
Deleting a File or Directory
IsolatedStorageFile class also provides a method to delete a
file in which you are no longer interested:
' Delete some files isf.DeleteFile("Dir3/Dir4/Anotherfile.txt") isf.DeleteFile("Rootfile.txt")
You can also delete directories by using the
method. Note that you must delete nested directories one at a time, starting
with the most deeply nested directory.
' Delete some directories isf.DeleteDirectory("Dir1/Dir2/") isf.DeleteDirectory("Dir1/")
Pages: 1, 2