Hello.. I hope all are enjoying Windows Phone 7 development.. This is 1 more blog on Windows Phone 7. This blog is regarding isolated storage of Windows Phone 7. In this blog I am going to include a brief introduction on isolated storage of Windows Phone 7 and 1 demo along with its screenshots.
Isolated storage is basically local storage of your Windows Phone 7 mobile. Your applications can save its data in this isolated storage. It is compulsory for the user to save their application data in isolated storage. Windows Phone 7 does not allow storing files in any other file system locations.
Let’s see what exactly isolated storage means. “Isolated” means separated from others. So basically, in this storage each application’s data is isolated from each other. Means 1 application can’t see and access another application’s data. But sometimes it may happen that your 1 application may need data of another application. Then the best option is use of Cloud. You can put your data in cloud and access whichever data you want for any application.
It is very important that developer should keep a watch on data storage. It’s important that you should conserve data space. You can do this by deleting temporary data in storage. Also if user creates any data in the application, developer should allow them to delete it later which will help to conserve space. Using cloud as option is another way of conserving data space. Always give user, information about data getting stored in storage through your application so that if storage is getting full, user will get to know it and accordingly user can delete files.
The above figure is taken from msdn site. You can refer msdn site
http://msdn.microsoft.com/en-us/library/ff402541%28v=VS.92%29.aspx for this. This figure shows you the actual storage of data. Figure is self-explanatory. Upper is the main Application Data Store Folder. It has two parts local settings and files and folders structure. 1st is Local Settings which updates the changes made by developer to the application directory. 2nd part is Files and Folders. You can access it by using API. Using these APIs developer can create, delete and manipulate files and folders.
Now let’s see demo for isolated storage.
Step 1:-
As we have to develop windows phone application, we need to choose Windows Phone application in Visual Studio. So it will be File -> New -> Project -> Windows Phone Application.
Once you create a new project you will get your project on your screen as shown in below figure.
Step 2:-
In this demo I am going to show you how to write and store data in isolated storage and also how to read data from isolated storage.
For that I am going to use 1 textbox to take input from user, 1 button to save data, 1 textblock to display stored data and again 1 button to retrieve data from isolated storage and display in above textblock. You can see this UI in figure below.
Step 3:-
Code behind..
As we have to use isolated storage, we need to add 2 IO namespaces.
System.IO
System.IO.IsolatedStorage
So at start you add these namespaces by including using statement in your code behind.
Step 4:-
Here we are going to include the steps for writing and reading data from isolated storage.
In the above figure you can see two methods written btnSave_Click and btnShow_Click. btnSave_Click is written for writing and storing data in isolated storage and btnShow_Clickis written for displaying data from isolated storage.
Let’s start with storing data. Here IsolatedStorageFile represents an isolated storage area containing files and folders. So basically using IsolatedStorageFile we can create directories, files etc were we can store our data. Later on we have created a directory called Data in storage. We can create files in this directory and store data in it. After that we going to use StreamWriter to write data in a file. So what we actually doing here is, 1st we creating a new IsolatedStorageFileStream for a file abc.txt in above created folder. Mode of this file is Create as we going to create this file in above created store (isf in our code). This whole argument is been given to StreamWriter. Later on we going to write in this file whatever input we had taken from the user. Thus we are done with writing data in file.
Now let’s see how to retrieve data. Here again we declaring new IsolatedStorageFile same way as above. Here as we have to read data we going to use StreamReader. So similarly, creating a new IsolatedStorageFileStream for a file abc.txt in above created folder. Mode of this file is Open as we going to open this file which was already created above in our store (isf in our code). This whole argument is been given to StreamReader. After this, we reading data from that file and displaying it in textblock. Thus we are done with reading data in file.
Step 5:-
Now let’s run our application by pressing F5. You will get on screen as shown in below figure.
This is going to be the UI which we had designed.
Now let’s enter input in textbox which will be stored in isolated storage.
Once you click Save button your data gets saved in isolated storage and you get a message box that your data is stored.
Now let’s retrieve this stored data. Click on Show button and you will get the stored data in your textblock as shown in below figure.
That’s it!!! We are done with our isolated storage demo. Will be back soon with such more interesting blogs. Till that time, Good bye and enjoy WP7 developing.