簡體   English   中英

如何在Silverlight中獲得隔離存儲的路徑?

[英]How to get path of isolated storage in silverlight?

在這里讀過類似的帖子。 我嘗試實現它,但出現異常提示

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

我要去哪里錯了? 請幫我..

對於那些具有較高權限的用戶(尤其是在瀏覽器中),我想出了一個半功能解決方案來確定路徑。 不幸的是,如果您從dev切換到live,您將看到一個不同的文件夾路徑,因此您必須將其包括在此處,還應該通過傳入的page參數或通過添加一個檢查來確定您正在運行哪個版本(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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM