none
Isolated Storage

    Question

  • Is it possible to find out the physical location on a client machine of the isolated storage the .NET framework has just created?

    I have an embedded .NET control and for security reasons I want to use the isolated storage.  However, the control may launch the files in an external application, which will need to know the full path.

    I've Google'd this and found that the lcoation is different depending on the version of Windows and method used.  Using the QuickWatch window in VS.NET I can see the actual physical location, buried in the non-public members, but there doesn't appear to be a way to get to it.

    Like I said above, is there any way to get this?  If not, I'll have to use another method.

    Incidentally, I've tried the Path.GetTempPath() but this fails due to the CAS settings.  I have a policy defined based on strong names giving any assembly full trust with that strong name.  It appears that it gives me a restricted set of permissions which is clearly not "full trust".  If I use another method, say site or URL, I do get full trust and the Path.GetTempPath() actually works as expected.

    TIA,

    Haydn
    Monday, August 06, 2007 1:18 PM

Answers

  • Hi Haydn,

     

    You can use reflection to get the private file path, just like the following code sample:

    Code Snippet

    using System.IO.IsolatedStorage;
    using System.IO;
    using System.Diagnostics;
    using System.Reflection;
    using System.Collections.Generic;
    using System.Text;

     

    const string ISOLATED_FILE_NAME = "MyIsolatedFile.txt";

    IsolatedStorageFile isoStore =
      IsolatedStorageFile.GetStore(IsolatedStorageScope.User
      | IsolatedStorageScope.Assembly, null, null);

    string[] fileNames = isoStore.GetFileNames(ISOLATED_FILE_NAME);

    foreach (string file in fileNames)
    {
        if (file == ISOLATED_FILE_NAME)
        {
            Debug.WriteLine("The file already exists!");
        }
    }

    IsolatedStorageFileStream oStream =
      new IsolatedStorageFileStream(ISOLATED_FILE_NAME,
      FileMode.Create, isoStore);

     

    // Get the physical path using reflection

    String filePath = oStream.GetType().GetField("m_FullPath",
        BindingFlags.Instance | BindingFlags.NonPublic).GetValue(oStream).ToString();
    Console.WriteLine(filePath);

     

     

    You said: "the control may launch the files in an external application, which will need to know the full path.".  Why not directly expose the IsolatedStorageFileStream or a StreamReader to the external application to let it access the file?

     

    Thanks!

    Wednesday, August 08, 2007 9:16 AM

All replies

  • Hi Haydn,

     

    You can use reflection to get the private file path, just like the following code sample:

    Code Snippet

    using System.IO.IsolatedStorage;
    using System.IO;
    using System.Diagnostics;
    using System.Reflection;
    using System.Collections.Generic;
    using System.Text;

     

    const string ISOLATED_FILE_NAME = "MyIsolatedFile.txt";

    IsolatedStorageFile isoStore =
      IsolatedStorageFile.GetStore(IsolatedStorageScope.User
      | IsolatedStorageScope.Assembly, null, null);

    string[] fileNames = isoStore.GetFileNames(ISOLATED_FILE_NAME);

    foreach (string file in fileNames)
    {
        if (file == ISOLATED_FILE_NAME)
        {
            Debug.WriteLine("The file already exists!");
        }
    }

    IsolatedStorageFileStream oStream =
      new IsolatedStorageFileStream(ISOLATED_FILE_NAME,
      FileMode.Create, isoStore);

     

    // Get the physical path using reflection

    String filePath = oStream.GetType().GetField("m_FullPath",
        BindingFlags.Instance | BindingFlags.NonPublic).GetValue(oStream).ToString();
    Console.WriteLine(filePath);

     

     

    You said: "the control may launch the files in an external application, which will need to know the full path.".  Why not directly expose the IsolatedStorageFileStream or a StreamReader to the external application to let it access the file?

     

    Thanks!

    Wednesday, August 08, 2007 9:16 AM
  • I tried to use reflection to get real file path in Silverlight 2.0 application: 

    String filePath = oStream.GetType().GetField("m_FullPath",
        BindingFlags.Instance | BindingFlags.NonPublic).GetValue(oStream).ToString();


    I got error: 
    "An exception of type 'System.FieldAccessException' occurred in mscorlib.dll but was not handled in user code

    Additional information: System.IO.IsolatedStorage.IsolatedStorageFileStream.m_FullPath".

    VS2008 in debug shows value of this field, but my code not. What can I do to solve it?

    Tuesday, March 25, 2008 8:39 AM
  • Reflection in Silverlight has lots of limitations, for more details, you can ask in the Silverlight: Forums.
    Tuesday, March 25, 2008 8:43 AM
  • what is this "m_FullPath"..?

    singh123456

    Friday, January 18, 2013 10:02 AM