Windows Phone 7 : Code Snippet: Enumerate Files in Isolated Storage

This sample code uses the IsolatedStorageFile class to create a few test directories and populates them with files.  Then it recursively enumerates the files and directories, displaying the output in the debug window.

  1. public void EnumerateIsolatedStorageFiles()
  2. {
  3.     try
  4.     {
  5.         // Get an isolated store for this applicaiton and put it into an
  6.         // IsolatedStorageFile object.
  7.  
  8.         using (IsolatedStorageFile isoStore =
  9.             IsolatedStorageFile.GetUserStoreForApplication())
  10.         {
  11.             // Create sub directories and create files
  12.             // in them to test enumeration
  13.             isoStore.CreateDirectory("A");
  14.             IsolatedStorageFileStream streamA =
  15.                 new IsolatedStorageFileStream(@"A\TestFileA.Txt", FileMode.Create, isoStore);
  16.  
  17.             isoStore.CreateDirectory("B");
  18.             IsolatedStorageFileStream streamB =
  19.                 new IsolatedStorageFileStream(@"B\TestFileB.Txt", FileMode.Create, isoStore);
  20.  
  21.             isoStore.CreateDirectory("C");
  22.             IsolatedStorageFileStream streamC =
  23.                 new IsolatedStorageFileStream(@"C\TestFileC.Txt", FileMode.Create, isoStore);
  24.  
  25.             isoStore.CreateDirectory(@"C\1");
  26.             IsolatedStorageFileStream streamC1 =
  27.                 new IsolatedStorageFileStream(@"C\1\TestFileC1.Txt", FileMode.Create, isoStore);
  28.  
  29.             isoStore.CreateDirectory(@"D\1\2\3\4\5");
  30.             IsolatedStorageFileStream streamD =
  31.                 new IsolatedStorageFileStream(@"D\1\2\3\4\5\TestFileD.Txt", FileMode.Create, isoStore);
  32.  
  33.             streamA.Close();
  34.             streamB.Close();
  35.             streamC.Close();
  36.             streamC1.Close();
  37.             streamD.Close();
  38.  
  39.             // There might be a small delay between when the above code
  40.             // executes and when the files are created in the store.
  41.             // Closing and opening the store in this example ensures that
  42.             // the common language runtime has finished creating the files.
  43.         } // using
  44.  
  45.         using (IsolatedStorageFile isoStore =
  46.             IsolatedStorageFile.GetUserStoreForApplication())
  47.         {
  48.             // List files in root
  49.             foreach (string file in isoStore.GetFileNames())
  50.             {
  51.                 Debug.WriteLine(file);
  52.             }
  53.             // Recursively enumerate sub-directories and list contents
  54.             GetFiles(isoStore, @"\");
  55.         } // using
  56.     }
  57.     catch (IsolatedStorageException e)
  58.     {
  59.         Debug.WriteLine(e.Message);
  60.     }
  61. }
  62.  
  63. // Recursively enumerate sub-directories
  64. void GetFiles(IsolatedStorageFile isoStore, string directory)
  65. {
  66.     string[] DirNames;
  67.     if (directory == @"\")
  68.         DirNames = isoStore.GetDirectoryNames();
  69.     else
  70.         DirNames = isoStore.GetDirectoryNames(directory + @"\*");
  71.  
  72.     foreach (string dir in DirNames)
  73.     {
  74.         string mDir =
  75.             directory == @"\" ? directory + dir : directory + @"\" + dir;
  76.         foreach (string file in isoStore.GetFileNames(mDir + @"\*.*"))
  77.         {
  78.             Debug.WriteLine(file);
  79.         }
  80.         GetFiles(isoStore, mDir);
  81.     }
  82. }

 

Enjoy!

Mike

This work is licensed under a Creative Commons license.