Skip to content

Isolated Storage in C#

As I am working in a Windows Application, client asked about the Windows 7 and Windows Vista compatibility. As I was writing only managed code I was not worried about the compatibility and most of the time it was working fine. (I was using Windows Vista). Today evening one of my colleague was testing the application on his Windows 7 machine, the application was crashing :( After initial diagnostics I was able to find the solution, it was because of Tracing. I was using TextWriterTraceListener,from System.Diagnostics namespace, and the File was created in the application path.Because he installed the application in C:\Program Files\AppFolder, Windows was denied access to write the File. The easily solution was, Right Click on the Application, Select RUNAS option, select Administrator. The other solution was writing the Log File in some temporary location, and use it. Then I found some nice feature in .Net called Isolated Storage. It is helpful to Run your application by less privileged users. With these stores, you can read and write data that less trusted code cannot access and prevent the exposure of sensitive information that can be saved elsewhere on the file system. Data is stored in compartments that are isolated by the current user and by the assembly in which the code exists. Additionally, data can be isolated by domain. Roaming profiles can be used in conjunction with isolated storage so isolated stores will travel with the user’s profile.

We can access the Isolated Storage related classes from System.IO.IsolatedStorage namespace.

//Getting the User scoped Store corresponding to the calling codes assembly identity.
//IsolatedStorageFile class provides basic functionality to create files or folders.
IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly();
//Checking whether Directory Exists. Directory.Exists will not work. Also do some wildcard
//search like "dotnet*" or "dotnet?"
string[] directories = isolatedStorageFile.GetDirectoryNames("dotnetthoughts");
//Creating the directory if it is not exists.
if (directories.Length == 0)
{
    isolatedStorageFile.CreateDirectory("dotnetthoughts");
}
//IsolatedStorageFileStream encapsulates stream, used to create files
IsolatedStorageFileStream isolatedStorageFileStream =
    new IsolatedStorageFileStream(@"dotnetthoughts\\System.log",
        FileMode.OpenOrCreate, isolatedStorageFile);
//Writing the content to IsolatedStorage.
using (StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream))
{
    streamWriter.WriteLine("Hello Isolated Storage");
}
//Reading the contents from Isolated storage – If you try to read like this it will throw //exception.(Stream is not Readable)
using (StreamReader streamReader = new StreamReader(isolatedStorageFileStream))
{
    MessageBox.Show(streamReader.ReadToEnd());
}
isolatedStorageFileStream.Close();
isolatedStorageFile.Close();

There is some permission attributes too, which is used to grant code to access IsolatedStorage.


[IsolatedStorageFilePermission(SecurityAction.Demand)]
static class Program
{
//Code
}

The IsolatedStorage can be used in Silverlight assemblies too, which will helps to access local file system from Silverlight.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*