Can I get a path for a IsolatedStorage file and read it from external applications?

人走茶凉 提交于 2019-11-27 01:41:27

问题


I want to write a file where an external application can read it, but I want also some of the IsolatedStorage advantages, basically insurance against unexpected exceptions. Can I have it?


回答1:


You can retrieve the path of an isolated storage file on disk by accessing a private field of the IsolatedStorageFileStream class, by using reflection. Here's an example:


// Create a file in isolated storage.
IsolatedStorageFile store = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
IsolatedStorageFileStream stream = new IsolatedStorageFileStream("test.txt", FileMode.Create, store);
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("Hello");
writer.Close();
stream.Close();

// Retrieve the actual path of the file using reflection.
string path = stream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream).ToString();

I'm not sure that's a recommended practice though.

Keep in mind that the location on disk depends on the version of the operation system and that you will need to make sure your other application has the permissions to access that location.




回答2:


I use Name property of FileStream.

private static string GetAbsolutePath(string filename)
{
    IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

    string absoulutePath = null;

    if (isoStore.FileExists(filename))
    {
        IsolatedStorageFileStream output = new IsolatedStorageFileStream(filename, FileMode.Open, isoStore);
        absoulutePath = output.Name;

        output.Close();
        output = null;
    }

    return absoulutePath;
}

This code is tested in Windows Phone 8 SDK.




回答3:


Instead of creating a temp file and get the location you can get the path from the store directly:

var path = store.GetType().GetField("m_RootDir", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(store).ToString();


来源:https://stackoverflow.com/questions/1112681/can-i-get-a-path-for-a-isolatedstorage-file-and-read-it-from-external-applicatio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!