天天看點

Windows Phone 8 檔案操作

在Windows Phone 8 中,操作檔案的方式限制很大,對獨立存儲中的檔案通路,可以采取兩種形式:

1、IsolatedStorageFile file = IsolatedStorageFile .GetUserStoreForApplication();

2、StorageFolder folder = Windows.Storage.ApplicationData .Current.LocalFolder;

     IReadOnlyList<StorageFile > files = await folder.GetFilesAsync();

兩者取到的檔案夾路徑是一緻的,都是 "C:\Data\Users\DefApps\AppData\{7315D4B4-02D4-452C-A618-08281E0192F1}\Local",大括号括起來的部分是根據應用程式變化而變化的,是應用程式的ProductID

如下圖:

Windows Phone 8 檔案操作

3、IsolatedStorageFile file = IsolatedStorageFile .GetUserStoreForApplication();

     file.CreateDirectory("ddd");

     file.CreateFile("ddd\\ds");

     file.CreateFile("sss");

這段代碼在Local檔案夾下建立了一個ddd檔案夾和sss一個檔案,在ddd檔案夾下面建立了一個ds檔案。

當使用

     string[] files = file.GetFileNames();

擷取獨立存儲檔案下面的檔案時,隻能擷取到目前檔案夾下面的檔案,對于子檔案夾的檔案,無法擷取到,隻能通過周遊子檔案夾來擷取子檔案夾下面的 檔案。

對于file.GetDirectoryNames(),可以擷取到一個“Shared”檔案夾,這個檔案夾的作用我還不知道- -!

Windows Phone 8 檔案操作

4、SD卡

     ExternalStorageDevice externalStorageDevice = sdCards.FirstOrDefault();

     //ExternalStorageFolder sdFolder = await externalStorageDevice.GetFolderAsync("d:\\data1");

     ExternalStorageFolder rootFolder = externalStorageDevice.RootFolder;

     IEnumerable<ExternalStorageFolder> subFolders = await rootFolder.GetFoldersAsync();

以上代碼結果參照下圖:

Windows Phone 8 檔案操作

可以發現SD卡的預設盤符為:“D:\\”。

擷取到的subFolders:隻有自己建立的檔案夾,對于系統檔案夾:WPSystem、Music、Pictures、Videos等,均無法擷取到。

參照下圖:

Windows Phone 8 檔案操作

可以看到在Windows Phone Runtime中,隻有LocalFolder被實作。

LocalFolder的所有成員如下圖:

Windows Phone 8 檔案操作

繼續閱讀