Working with Isolated Storage


Isolated storage is the solution to a unique problem that has arisen recently due to the increased security of users' desktops as well as the security enforced by the Common Language Runtime itself. All managed applications run within a security "sandbox" and this sandbox often prevents an application from creating files or accessing the user's hard disk at all. This security is highly desired, especially for applications obtained through the Internet to prevent malicious code from having unrestrained access to the user's hard disk.

However, for every malicious application for which the security is absolutely required, there are many more innocent applications that just want to use the disk storage to store things like user preferences. User preferences can be anything from the last position of a recently opened window to highly complex workflow and business rule options specific to the user. Also, components written by the same vendor may need to access shared data but might lack the security clearance necessary to access such data.

Isolated storage provides an area in which applications can store private information, such as user preferences, that is under the complete control of the end user. This gives the application the ability to read and write from a safe location to store information and gives the user the peace of mind of knowing that even if an application can save data, it can't do damage to any files other than the ones created by the application.

When you open an isolated storage location, your code must specify a scope for this store. This allows you to separate stores by user, by application, by domain, and even allows you to store the data within a user's roaming profile.

The code in Listing 7.4 shows an example of an application that stores and retrieves the user's favorite color. This preference will follow the user in his roaming profile if applicable.

Listing 7.4. Example of Isolated Storage

using System; using System.IO; using System.IO.IsolatedStorage; using System.Collections.Generic; using System.Text; namespace IsoSample { class Program { static void Main(string[] args) {     IsolatedStorageFile isoFile =         IsolatedStorageFile.GetStore(IsolatedStorageScope.Assembly |         IsolatedStorageScope.User |         IsolatedStorageScope.Roaming,null, null) ;     string curColor = "(none)";     IsolatedStorageFileStream isoFs =         new IsolatedStorageFileStream("favColor.txt", FileMode.OpenOrCreate,         FileAccess.ReadWrite);     if (isoFs.Length > 0)     {         byte[] fileBytes = new byte[isoFs.Length];         isoFs.Read(fileBytes, 0, (int)isoFs.Length);         curColor = ASCIIEncoding.ASCII.GetString(fileBytes);     }     isoFs.Close();     Console.WriteLine("Current Favorite Color: {0}", curColor);     Console.Write("New favorite color: ");     string newColor = Console.ReadLine();     IsolatedStorageFileStream newFs =         new IsolatedStorageFileStream("favColor.txt", FileMode.Create);     newFs.Write(ASCIIEncoding.ASCII.GetBytes(newColor), 0, newColor.Length);     newFs.Close(); } } } 

The first time you run the preceding application, it will indicate that the user's current favorite color is "(none)". The next time you start the application, it will show you the color that you entered on the previous run. The data is being stored in the isolated storage area, an area that won't allow the application to do damage to the hard disk or important files.

When you store data in isolated storage on Windows XP for a user, it will be located in <drive>\Documents and Settings\<User>\Application Data for a roaming-enabled profile, and <drive>\Documents and Settings\<User>\Local Settings\Application Data for a non-roaming-enabled profile.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net