Isolated Storage

Isolated storage allows an assembly or application domain to have a reserved space in the file system to store sensitive yet persistent data. This is not completely secure, but portions of the .NET Framework Security use isolated storage to access and manipulate items in the store in a secure manner. After you obtain an isolated storage file (a store), you can use standard I/O facilities to read and write files and directories within the store. You don't have to worry about any other application corrupting your data because each application (application domain or assembly) accesses only its area in isolated storage. Listing 16.17 shows a sample of how to use isolated storage.

Listing 16.17. Using Isolated Storage
static void Main(string[] args)
{
    IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForAssembly();
    // Print some statistics about the store
    Console.WriteLine(string.Format( "Assembly Identity: {0} ", isf.AssemblyIdentity));
    Console.WriteLine(string.Format("Current Size: {0} ", isf.CurrentSize));
    Console.WriteLine(string.Format("Maximum Size: {0} ", isf.MaximumSize));
    Console.WriteLine(string.Format("Scope: {0} ", isf.Scope));
    // This code checks to see if the file already exists.
    string[] fileNames = isf.GetFileNames("TestStore.txt");
    foreach (string file in fileNames)
    {
        if(file == "TestStore.txt")
        {
            Console.WriteLine("The file already exists!");
            Console.WriteLine("Type "StoreAdm /REMOVE" at the command line to delete all
 Isolated Storage for this user.");

            // Exit the program.
            isf.Close();
            return;
        }
    }

    writeToFile(isf);

    Console.WriteLine("The file "TestStore.txt" contains:");

    // Call the readFromFile and write the returned string to the
    // console.

    Console.WriteLine(readFromFile(isf));
    // Exit the program.
    isf.Close();
}

// This method writes "Hello Isolated Storage" to the file.
private static void writeToFile(IsolatedStorageFile isoStore)
{
    // Declare a new StreamWriter.
    StreamWriter writer = null;

    // Assign the writer to the store and the file TestStore.
    writer = new StreamWriter(new IsolatedStorageFileStream("TestStore.txt", FileMode
.CreateNew,isoStore));

    // Have the writer write "Hello Isolated Storage" to the store.

    writer.WriteLine("Hello Isolated Storage");

    writer.Close();

    Console.WriteLine("You have written to the file.");
}

The readFromFile function has been omitted because the implementation is similar to writeToFile.

To use IsolatedStorage, call one of two static functions:

  • GetUserStoreForAssembly— This returns an IsolatedStorageFile that is specific to the calling assembly.

  • GetUserStoreForDomain— This returns an IsolatedStorageFile that is specific to the calling application domain.

After you have an IsolatedStorageFile, you can use it to create an IsolatedStorageFileStream. After you have a stream, you can use any of the IO facilities that takes a stream to read and write to the store.

You can administer the isolated storage with a utility called storeadm. After you run the program associated with Listing 16.17, invoke storeadm /list to get the following output:

Record #1
[Assembly]
<System.Security.Policy.Url version="1">
   <Url>file://D:/IsolatedStorage.exe</Url>
</System.Security.Policy.Url>

        Size : 1024

If you call storeadm with no arguments, you get a usage report that provides some of the options that you need to administer the store. One of the most useful options is storeadm/remove, which allows you to remove the store completely.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.248.208