Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

Windows Phone 7 development: Using isolated storage

In my previous posting about Windows Phone 7 development I showed how to use WebBrowser control in Windows Phone 7. In this posting I make some other improvements to my blog reader application and I will show you how to use isolated storage to store information in phone.

Why isolated storage?

Isolated storage: Logical folder structureIsolated storage is place where your application can save its data and settings. The image on right (that I stole from MSDN library) shows you how application data store is organized.

You have no other options to keep your files besides isolated storage because Windows Phone 7 does not allow you to save data directly to other file system locations.

From MSDN: “Isolated storage enables managed applications to create and maintain local storage. The mobile architecture is similar to the Silverlight-based applications on Windows. All I/O operations are restricted to isolated storage and do not have direct access to the underlying operating system file system. Ultimately, this helps to provide security and prevents unauthorized access and data corruption.

Saving files from web to isolated storage

I updated my RSS-reader so it reads RSS from web only if there in no local file with RSS. User can update RSS-file by clicking a button. Also file is created when application starts and there is no RSS-file. Why I am doing this? I want my application to be able to work also offline. As my code needs some more refactoring I provide it with some next postings about Windows Phone 7. If you want it sooner then please leave me a comment here.

Here is the code for my RSS-downloader that downloads RSS-feed and saves it to isolated storage file calles rss.xml.


public class RssDownloader

{

    private string _url;

    private string _fileName;

 

    public delegate void DownloadCompleteDelegate();

    public event DownloadCompleteDelegate DownloadComplete;

 

    public RssDownloader(string url, string fileName)

    {

        _url = url;

        _fileName = fileName;

    }

 

    public void Download()

    {

        var request = (HttpWebRequest)WebRequest.Create(_url);

        var result = (IAsyncResult)request.BeginGetResponse(ResponseCallback, request);       

    }

 

    private void ResponseCallback(IAsyncResult result)

    {

        var request = (HttpWebRequest)result.AsyncState;

        var response = request.EndGetResponse(result);

 

        using(var stream = response.GetResponseStream())

        using(var reader = new StreamReader(stream))

        using(var appStorage = IsolatedStorageFile.GetUserStoreForApplication())

        using(var file = appStorage.OpenFile("rss.xml", FileMode.OpenOrCreate))

        using(var writer = new StreamWriter(file))

        {

            writer.Write(reader.ReadToEnd());

        }

 

        if (DownloadComplete != null)

            DownloadComplete();

    }

}


Of course I modified RSS-source for my application to use rss.xml file from isolated storage. As isolated storage files also base on streams we can use them everywhere where streams are expected.

Reading isolated storage files

As isolated storage files are opened as streams you can read them like usual files in your usual applications. The next code fragment shows you how to open file from isolated storage and how to read it using XmlReader. Previously I used response stream in same place.


using(var appStorage = IsolatedStorageFile.GetUserStoreForApplication())

using(var file = appStorage.OpenFile("rss.xml", FileMode.Open))

{

    var reader = XmlReader.Create(file);               

 

    // more code

}


As you can see there is nothing complex. If you have worked with System.IO namespace objects then you will find isolated storage classes and methods to be very similar to these. Also mention that application storage and isolated storage files must be disposed after you are not using them anymore.

Comments

Jason Farrell said:

What happens if two apps use the same file name?

# November 3, 2010 9:23 PM

Chris Stevenson said:

re: What happens if two apps use the same file name?

ans: Can't happen. Each app has its own seperate ("isolated") place in the file system, and can not go outsie of it. Besides, apps are tombstoned when not active.

# November 20, 2010 11:21 PM

Mitchell said:

(newbie here)

where do you put the xml url thats downloaded?

# July 26, 2011 2:16 PM

Wholesale sunglasses said:

Thanks for taking this opportunity to converse about  this, I feel strongly  about this and I enjoy learning about this subject.

# August 10, 2011 5:06 AM

Buy oem software online said:

8PbAln Uh, well, explain me a please, I am not quite in the subject, how can it be?!...

# September 23, 2011 12:52 PM