0

我看過類似的帖子here..。我試圖實現它,但得到一個異常說如何在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; 
    } 

我要去哪裏錯了嗎?請幫助我..

+0

我也想知道什麼「m_FullPath」是? – Mangesh

+0

是您的應用程序OOB嗎?如果您的應用程序不在瀏覽器外面,您無法獲得獨立存儲的完整路徑 - 我嘗試了很多次,失敗了! – DNKROZ

+1

@DNKROZ:你說得對。尋找它後,我得出了同樣的結論。但是在silverlight 5中,即使應用程序在瀏覽器中運行,也可以訪問本地文件系統。但爲此,您需要爲您的應用程序提供更高的信任度。請參閱此[MSDN鏈接。](http://msdn.microsoft.com/en-us/library/gg192793(v = vs.95).aspx) – Mangesh

回答

0

對於具有提升權限的用戶(特別是在瀏覽器中),我提出了一個半功能解決方案來確定路徑。不幸的是,如果你從dev切換到live,你會看到一個不同的文件夾路徑,所以你必須在這裏包含它,也應該通過傳入的頁面參數來添加一個檢查你運行的版本(dev或live)主機URL左右

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); 
    } 
} 
+0

寫完這些之後,我回去設計了一個不同的解決方案,它基於放置一個沒有內容但文件名爲時間戳。然後我會搜索該基本路徑上的所有silverlight文件夾以獲取正確的時間戳文件,然後抓取父文件夾。 –

相關問題