Home   Subscribe   Linkedin
  Archive Contact  

Isolated Storage

The following example demonstrates how to save or retrieve files in isolated storage.

use the IsolatedStorageFile class. IsolatedStorageFile represents an isolated storage area that contains files and directories. When you work with files, you also typically use the IsolatedStorageFileStream class to read and write content to the file.

Step 1: Obtain the isolated storage for an application ,create a directory at the root of the store and create a file .

private void StoreData(string firstName,string middleName,string lastName)
       {
           if(string.IsNullOrEmpty(firstName)) return;
           // Obtain the isolated storage for an application.
           using (var store = IsolatedStorageFile.GetUserStoreForApplication())
           {
               // Create a directory at the root of the store.
               if (!store.DirectoryExists("UserInfo"))
               {
                   store.CreateDirectory("UserInfo");
               }

           
               // create a file and get its stream.
               var fileName = @"UserInfo\" + firstName + ".xml";
               if (!store.FileExists(fileName))
               {
                   var storageFileStream = store.CreateFile(fileName);
                   storageFileStream.Close();
               }
               // Get the stream of an existing file
               using (var streamWriter = new StreamWriter(store.OpenFile(fileName, FileMode.Open)))
               {
                   var xDocument = new XDocument(

                                      new XElement("User",
                                        new XElement("FirstName", firstName),
                                        new XElement("MiddleName", middleName),
                                        new XElement("LastName", lastName))
                       );
                   // Write to the stream.
                   streamWriter.Write(xDocument);
               }
           }
       }

 

 

Step 2: Get Stored Data

private string GetData(string firstName)
      {
          if(string.IsNullOrEmpty(firstName)) return null;

          // Obtain the isolated storage for an application.
          using (var store = IsolatedStorageFile.GetUserStoreForApplication())
          {
              var result = string.Empty;
              if (!store.DirectoryExists("UserInfo")) return result;

 

              var fileName = @"UserInfo\" + firstName + ".xml";
              if (!store.FileExists(fileName)) return result;

              using ( var storageFileStream = store.OpenFile(fileName, FileMode.Open))
              {
                  var xDocument = XDocument.Load(storageFileStream);

                  result += "FirstName : " + xDocument.Element("User").Element("FirstName").Value + "\n";
                  result += "MiddleName : " + xDocument.Element("User").Element("MiddleName").Value + "\n";
                  result += "LastName : " + xDocument.Element("User").Element("LastName").Value;
              }
              return result;
          }
      }

 

 I have attached the sample application along with this post

IsolatedStorageDemo.zip (21.33 kb)

Posted by: Mohd Ishaq

Categories: C#, Silverlight

Tags: