10.4 Isolated Storage
The .NET Framework's isolated
storage classes create and manipulate
data compartments that are unique to a user and an assembly. For
example, suppose that the user joe runs the .NET
application someapp.exe. If
someapp.exe uses isolated storage to create
directories and files, the .NET runtime guarantees that another
program (such as anotherapp.exe) cannot access
those files, even if joe runs
anotherapp.exe. Further, if another user,
bob, runs someapp.exe, he
won't be able to access the files that
someapp.exe created when
joe ran it.
Isolated storage is limited by a quota, so even if
it's manipulated by untrusted mobile code, you
don't need to worry about that code performing a
denial-of-service attack by filling your disk with useless data.
Where does isolated storage live? This depends on your operating
system, but in Windows 2000 and XP, it is either found in
\Documents and
Settings\<username>\Local Settings\Application
Data\Microsoft\IsolatedStorage (the nonroaming profile) or
in \Documents and
Settings\<username>\Application
Data\Microsoft\IsolatedStorage (the roaming profile). For
other operating systems, such as Windows NT 4.0 or Windows 98, see
Introduction to Isolated Storage in the .NET
Framework SDK documentation.
10.4.1 Reading and Writing Isolated Storage
To create or access a
file in isolated storage, create an
instance of IsolatedStorageFileStream (pass in the
desired filename and combination of FileMode
constants). If your assembly
doesn't
currently have an area of isolated storage set up, .NET creates it at
this time. The following example creates a new file in isolated
storage, writes some data to it, and then displays the contents of
that file:
using System;
using System.IO;
using System.IO.IsolatedStorage;
class Isolated {
static void WriteToIS( ) {
// Create a file in isolated storage
IsolatedStorageFileStream stm =
new IsolatedStorageFileStream("test.txt", FileMode.Create);
// Write some text to the stream
StreamWriter sw = new StreamWriter(stm);
sw.WriteLine("Hello, World");
sw.Close( );
stm.Close( );
}
static void ReadFromIS( ) {
// Open an existing file in isolated storage
IsolatedStorageFileStream stm =
new IsolatedStorageFileStream("test.txt", FileMode.Open);
// Read the file
StreamReader sr = new StreamReader(stm);
string s = sr.ReadToEnd( );
sr.Close( );
stm.Close( );
Console.WriteLine(s);
}
static void Main( ) {
WriteToIS( );
ReadFromIS( );
}
}
After running this sample you will find the resulting file in the
appropriate IsolatedStorage folder for your
operating system, as described in the previous section.
10.4.2 Enumerating the Contents of Isolated Storage
The .NET Framework includes
the storadm.exe
utility for listing (/LIST) the current
user's isolated storage or for removing
(/REMOVE) all of the user's
isolated storage.
You can also access the current user's stores using
IsolatedStorageFile. Use the static
IsolatedStorageFile.GetEnumerator( ) method with
an argument of IsolatedStorageScope.User to
enumerate all the isolated storage for the current user. Once you
have a store, you can inspect it or modify it, or open one of the
files it contains by passing the store to the
IsolatedStorageFileStream constructor.
using System;
using System.Collections;
using System.IO;
using System.IO.IsolatedStorage;
using System.Security.Policy;
class EnumIsolated {
static void Main( ) {
// Get all the current user's isolated stores
IEnumerator ie =
IsolatedStorageFile.GetEnumerator(IsolatedStorageScope.User);
while (ie.MoveNext( )) {
IsolatedStorageFile isf = (IsolatedStorageFile) e.Current;
// Find the location of the associated assembly
Url url = (Url)isf.AssemblyIdentity;
Console.WriteLine("Assembly: {0}", url.Value);
Console.WriteLine("Size: {0}", isf.CurrentSize);
// List all the files in the store
Console.WriteLine("Files:");
string[ ] files = isf.GetFileNames("*");
foreach (string file in files) {
IsolatedStorageFileStream isfs =
new IsolatedStorageFileStream(file, FileMode.Open, isf);
Console.WriteLine(" {0}\t{1} bytes", file, isfs.Length);
}
}
}
}
|