如何在Silverlight中获取隔离存储的路径?

时间:2014-04-07 06:34:28

标签: silverlight isolatedstorage lync-2013 isolatedstoragefile

我读了一篇类似的帖子here..。我尝试实现它,但得到一个例外说

Attempt by method 'get_path_isolated.Page.button1_Click(System.Object, System.Windows.RoutedEventArgs)' to access field 'System.IO.IsolatedStorage.IsolatedStorageFileStream.m_FullPath' failed.

我有这段代码

public void button1_Click(object sender, RoutedEventArgs e)
{
    isoStore = IsolatedStorageFile.GetUserStoreForApplication();
    isoStore.CreateDirectory("root_dir");
    IsolatedStorageFileStream iostream = new IsolatedStorageFileStream("sampleFile.txt", FileMode.Create, isoStore);
    StreamWriter writer = new StreamWriter(iostream);
    writer.Write("jaimokar");

    try
    {
        FieldInfo pi = iostream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic);
        string path = pi.GetValue(iostream).ToString();
    }
    catch (Exception ex)
    {
        textBox1.Text += ex.Message;
    }
我出错的地方?请帮帮我..

1 个答案:

答案 0 :(得分:0)

对于那些具有提升权限的用户(特别是在浏览器中),我提出了一个确定路径的半功能解决方案。不幸的是,如果你从开关切换到实时,你会看到一个不同的文件夹路径,所以你必须在这里包含它,你也应该通过传入的页面参数或者添加检查你正在运行的版本(dev或live)主持人网址

private string GetProfilePath() {
var fullname = string.Empty;
try
{
    // ReSharper disable IdentifierTypo
    // ReSharper disable CommentTypo
    var profilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
    profilePath = Path.GetDirectoryName(profilePath);
    profilePath = Path.Combine(profilePath, @"LocalLow\Microsoft\Silverlight\is");
    //profilePath = Path.Combine(profilePath, IsoPathStaging + "\\");
    var directoryInfo = new DirectoryInfo(profilePath); // C:\Users\<username>\AppData\LocalLow\Microsoft\Silverlight\is --constant
    var dirs = directoryInfo.EnumerateDirectories();
    // ReSharper disable PossibleMultipleEnumeration
    fullname = "1: " + dirs.First().FullName;
    dirs = dirs.First().EnumerateDirectories(); // \ir5ffeej.4of --random
    fullname = "2: " + dirs.First().FullName;
    dirs = dirs.First().EnumerateDirectories(); // \lfab1wva.xmb --random
    fullname = "3: " + dirs.First().FullName;
    dirs = dirs.First().EnumerateDirectories(); // \1 --constant
    fullname = "4: " + dirs.First().FullName;
    dirs = dirs.Where(d => d.Name == "s"); // \s --constant
    fullname = "5: " + dirs.First().FullName;
    var dirs2 = dirs.First().EnumerateDirectories()
        .Where(d => d.Name == "sbudlbc2oqx0eo0odi5nzpo2qppp3zmxxxxxxxxxxxxxxxxxxxxxxxxx").ToList(); // \<dev dir> --constant-ish
    if (!dirs2.Any())
    {
        dirs2 = dirs.First().EnumerateDirectories()
            .Where(d => d.Name == "2gbsxl5no1wzqebnzbj2wglhi33za1rxxxxxxxxxxxxxxxxxxxxxxxxx").ToList(); // \<live dir> --constant-ish
    }
    if (!dirs2.Any())
    {
        throw new Exception("Unable to locate silverlight storage");
    }
    fullname = "6: " + dirs2.First().FullName;
    dirs = dirs2.First().EnumerateDirectories().Where(d => d.Name == "f"); // \f --constant
    fullname = "7: " + dirs.First().FullName;
    var dir = dirs.First(); // final
    fullname = dir.FullName;
    // ReSharper restore CommentTypo
    // ReSharper restore PossibleMultipleEnumeration
    return fullname;
    // ReSharper restore IdentifierTypo
}
catch (NotSupportedException ex)
{
    Debug.WriteLine(ex);
    MessageBox.Show(
        "Failed to run (Not Supported):"
        + Environment.NewLine + fullname
        + Environment.NewLine + ex.Message,
        messageBoxTitle,
        MessageBoxButton.OK);
    CheckElevatedPermissions();
    return string.Empty;
}
catch (Exception ex)
{
    Debug.WriteLine(ex);
    MessageBox.Show(
        "Failed to run:"
        + Environment.NewLine + fullname
        + Environment.NewLine + ex.Message,
        messageBoxTitle,
        MessageBoxButton.OK);
    return string.Empty;
}
}

同样,您必须拥有提升权限才能使用,稍后我会使用此路径查找文件以供其他用途:

var fullPath = Path.Combine(GetProfilePath(), FileName);
Run(fullPath.Replace("\\\\", "\\"));

private static void Run(string fullPath)
{
    try
    {
        CheckElevatedPermissions();
        var shell = AutomationFactory.CreateObject("WScript.Shell");
        shell.Run("\"" + fullPath + "\"");
    }
    catch (NotSupportedException ex)
    {
        Debug.WriteLine(ex);
        MessageBox.Show(
            "Failed to run (Not Supported):"
            + Environment.NewLine + fullPath
            + Environment.NewLine + ex.Message,
            messageBoxTitle,
            MessageBoxButton.OK);
        CheckElevatedPermissions();
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
        MessageBox.Show(
            "Failed to run:"
            + Environment.NewLine + fullPath
            + Environment.NewLine + ex.Message,
            messageBoxTitle,
            MessageBoxButton.OK);
    }
}