我可以获取IsolatedStorage文件的路径并从外部应用程序读取它吗?

时间:2009-07-11 01:31:57

标签: c# .net isolatedstorage

我想写一个外部应用程序可以读取它的文件,但我也想要一些IsolatedStorage优点,基本上可以防止意外异常。我能拥有吗?

3 个答案:

答案 0 :(得分:26)

您可以通过使用反射访问IsolatedStorageFileStream类的私有字段来检索磁盘上的独立存储文件的路径。这是一个例子:


// 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();

我不确定这是推荐的做法。

请记住,磁盘上的位置取决于操作系统的版本,并且您需要确保其他应用程序具有访问该位置的权限。

答案 1 :(得分:9)

我使用FileStream的Name属性。

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;
}

此代码在Windows Phone 8 SDK中进行了测试。

答案 2 :(得分:7)

您可以直接从商店获取路径,而不是创建临时文件并获取位置:

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