Tuesday, November 9, 2010

Reading And Writing Isolated Storage In Silverlight

In this post I will show you how you can create file on the Isolated Storage and also write on that file and also how can you check the size of the Isolated storage.Isolated storage is a mechanism that allows you to preserve data across browser sessions on a user’s machine. This storage area is tied to an individual user and helps you overcome the 4 KB limitation of a cookie. Unlike a cookie, isolated storage lies outside of the browser cache.
The code which is used to write in the Isolated Storage is listed in List 1.The class used for the Isolated Storage is IsolatedStorageFile which represents an isolated storage area containing files and directories. IsolatedStorageFile class has static method with the name GetUserStoreForApplication which is used to obtains user-scoped isolated storage corresponding to the calling code's application identity. After retrieving the user isolated storage object next step is to create the stream object which is used to write the contains.

using (IsolatedStorageFile isolatedStoragFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("MyFileName.txt", FileMode.Append, isolatedStoragFile)) { using (StreamWriter writer = new StreamWriter(stream)) { writer.Write("Message Date And Time: " + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") +" -> "+ txtTextToWrite.Text + "\r"); } stream.Close(); } }
List 1

IsolatedStorageFileStream is the class which Exposes a file within isolated storage.IsolatedStorageFileStream is used to read, write and create files in isolated storage. So if you want to read , write or create new file in the Isolated Storage area you need the object of the IsolatedStorageFileStream. I have passed the file name which in this case is .txt extension the, second parameter is the FileMode enumeration which has one of the values as listed in the table given below.The last parameter to the IsolatedStorageFileStream constructor is the IsolatedStorageFile object which is created before.

FileModeDescription
CreateNewAttempts to create a new file If a file of the same name exists, an IsolatedStorageException will be thrown. If there isn’t a preexisting file with the same name, a new, empty file will be created.
CreateA brute-force approach to creating a new file If a file of the same name exists, it’ll be overwritten. Either way, a new, empty file with the specified name will be created.
OpenAttempts to open a file with the given name If the file exists, the IsolatedStorageFileStream will have access to the file. If the file doesn’t exist, an IsolatedStorageException will be thrown.
OpenOrCreateOpens a file if it exists. If the file doesn’t exist, a new one will be created with the given name.
TruncateOpen an existing file and removes all its contents. This FileMode doesn’t allow read operations.
AppendOpens an existing file and prepares to add content onto the end
Table 1

After the creation of the IsolatedStorageFileStream object next is to use the StreamWriter class which take the IsolatedStorageFileStream object in the constructor and use the StreamWriter object to write the text or binary to the file.  The Image 1 show the layout of the example which I have used for the Isolated Storage. Here you can see that I have used two text boxes one for the input which user entered and second one is to read from the Isolated storage area which is shown in the Image 2.
Image 1


Image 2
The IsolatedStorageFile class has some useful properties which can be use to check the available free space (Gets a value that represents the amount of free space available for isolated storage.the value is in bytes), used space (Gets a value that represents the amount of the space used for isolated storage. the value is  in  bytes)and total allocated space to the current user.If you look at the example you can see that I have also print out the available free space and used space as well to show you how much space is available and how much is used.You can download the source code from here.

All and any comments / bugs / suggestions are welcomed!

1 comment:

Subhashree said...

Hi... Nice one..

I just want to clarify something. Here you mentioned

stream.Close();
within using block. I think when we will be out of using block, object will be disposed automatically. So we do not need to write that one. Am I right? :)