Get root directory for IsolatedStorageFiles

Sometimes you want to know the absolute path of a file that is persisted with IsolatedStorageFile. Apparently there is an internal property RootDirectory which contains this information:

public static class IsolatedStorageFileExtensions
{
 public static string GetRootDirectory(this IsolatedStorageFile isf)
 {
  var property = isf.GetType().GetProperty("RootDirectory", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetProperty);
  return (string)property.GetValue(isf, null);
 }
}

Here is a real world example of using SharpBITS.NET to download a file to IsolatedStorage:

class Program
{
 static void Main()
 {
  var mgr = new BitsManager();
  mgr.OnJobError += mgr_OnJobError;
  mgr.OnJobTransferred += mgr_OnJobTransferred;

  var job = mgr.CreateJob("job@" + DateTime.Now, JobType.Download);
  var src = @"http://localhost/";
  var dst = Path.Combine(GetIsfRoot(), "test.html");
  job.AddFile(src,dst);
  job.Resume();

  Console.WriteLine("running...");
  Console.ReadKey();
 }

 static void mgr_OnJobTransferred(object sender, NotificationEventArgs e)
 {
   e.Job.Complete();
   Console.WriteLine("completed: " + e.Job.DisplayName);
  }

 static void mgr_OnJobError(object sender, ErrorNotificationEventArgs e)
 {
  Console.WriteLine("error: " + e.Error.Description);
 }

 static string GetIsfRoot()
 {
  using (var f = IsolatedStorageFile.GetUserStoreForAssembly())
  {
   return f.GetRootDirectory();
  }
 }
}

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>