我想寫一個外部應用程序可以讀取它的文件,但是我還需要一些IsolatedStorage優勢,基本上可以防範意外的異常。我可以擁有它嗎?我可以獲得IsolatedStorage文件的路徑並從外部應用程序讀取它嗎?
15
A
回答
23
通過使用反射訪問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();
雖然我不確定這是一個建議的做法。
請記住,磁盤上的位置取決於操作系統的版本,您需要確保其他應用程序有權訪問該位置。
6
而不是創建一個臨時文件並獲得位置的,你可以直接從商店得到的路徑:
var path = store.GetType().GetField("m_RootDir", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(store).ToString();
8
我用的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;
}
此代碼已在Windows Phone 8 SDK中測試。
相關問題
- 1. 從Windows讀取用於應用程序的Xml文件路徑
- 2. 可以從外部xml文件讀取web.config文件嗎?
- 3. ClickOnce部署的應用程序可以從磁盤讀取文件嗎?
- 4. 我可以在FLASH中獲得shareobject文件路徑嗎?
- 5. 無法讀取文件絕對路徑(以應用程序包)
- 6. 我可以從外部捕獲程序調用和執行嗎?
- 7. 如何獲得最前端的應用程序的名稱使用AppleScript,並用它來獲取文件路徑
- 8. 我可以使用Kentico宏來獲取部分路徑嗎?
- 9. 我可以使用alivepdf從外部讀取存在的pdf嗎?
- 10. 從觸發應用程序的文件獲取路徑
- 11. 從其他應用程序獲取文件的路徑
- 12. 我們可以檢索安裝應用程序的路徑嗎?
- 13. 獲取應用程序路徑以創建新文件
- 14. 如何讀取路徑並獲得值
- 15. Swift從應用程序包以外的文件中讀取json
- 16. 在Centennial應用程序中獲取文件的文件路徑
- 17. 如何將文件添加到Android的Java應用程序,以便我可以得到它的路徑?
- 18. 從類路徑(Jar內部)的外部程序打開文件
- 19. 我可以預先取得課程路徑嗎?
- 20. 如何獲取應用程序路徑和讀取文本文件Windows Phone 7
- 21. 您可以暫停Java應用程序並從應用程序中獲取它的線程快照嗎?
- 22. 我可以得到一個.bbappend文件的路徑嗎?
- 23. 從Android中的外部存儲獲取文件的路徑
- 24. 一個應用程序可以從iPhone上的另一個應用程序訪問文件的路徑嗎?
- 25. 獲取僅合併Core圖形路徑的外部路徑
- 26. Corona sdk應用程序 - 它可以接受來自應用程序外部的文件嗎?
- 27. 如何從android中的外部存儲獲取文件路徑?
- 28. 用jquery讀取外部json文件的相對路徑方法
- 29. 我可以使用C#控制外部應用程序嗎?
- 30. 我可以使用Robotium關閉外部應用程序嗎?
至少在Silverlight 4中,任何嘗試執行此反射都會導致... mscorlib.dll中發生類型'System.FieldAccessException'的第一次機會異常 其他信息:嘗試按方法'Comms.MainPage.LayoutRoot_Loaded (System.Object,System.Windows.RoutedEventArgs)'以訪問字段'System.IO.IsolatedStorage.IsolatedStorageFile.m_StorePath'失敗。此外,它現在是「m_StorePath」而不是「m_FullPath」 - 更不用它的原因。 – DJA 2011-07-21 13:02:21
@DJA這是因爲安全考慮,您無法通過Silverlight中的反射獲得私人成員。 – ghord 2014-07-28 07:22:09