2014年8月30日 星期六

取得媒體庫的清單

在 Store Apps SDK 中有一個名為 KnownFolders 的 Class 可以取得許多系統的目錄,包括媒體庫及設備清單,官方文件:KnownFolders

雖然 Universal Apps 下可以使用同樣的 API 操作 Windows 及 Windows Phone,但還是有些 Property 只支援 Windows 這個平台,以這個例子就是 KnownFolders.DocumentsLibrary、KnownFolders.HomeGroup、KnownFolders.MediaServerDevices 這三個,比較麻煩的是編譯時期並不會提示,必須等到執行時才會有 Exception,所以撰寫 Universal Apps 時查閱文件與測試是很重要的習慣。

在查詢 KnownFolders 下的資料夾時,大部份都需要在專案的 Package.appxmanifest 加入對應的 Capabilities 例如 Music Library、Pictures Library、Removable Storage 等,否則在執行階段會發生 System.UnauthorizedAccessException 這個例外,以下示範以常用的 API 列出公共資料夾內的子目錄及檔案。

private void OnPageLoaded(object sender, RoutedEventArgs e)
{
    ShowFolder();
}

private async void ShowFolder()
{
    StorageFolder camerFolder = KnownFolders.CameraRoll;
    Debug.WriteLine("KnownFolder Name :: CameraRoll");
    await Enumerate(camerFolder, 1);

    StorageFolder musicFolder = KnownFolders.MusicLibrary;
    Debug.WriteLine("KnownFolder Name :: MusicLibrary");
    await Enumerate(musicFolder, 1);

    StorageFolder pictureFolder = KnownFolders.PicturesLibrary;
    Debug.WriteLine("KnownFolder Name :: PicturesLibrary");
    await Enumerate(pictureFolder, 1);

    StorageFolder removableDevices = KnownFolders.RemovableDevices;
    Debug.WriteLine("KnownFolder Name :: RemovableDevices");
    await Enumerate(removableDevices, 1);

    StorageFolder savedPictureFolder = KnownFolders.SavedPictures;
    Debug.WriteLine("KnownFolder Name :: SavedPictures");
    await Enumerate(savedPictureFolder, 1);

    StorageFolder videosFolder = KnownFolders.VideosLibrary;
    Debug.WriteLine("KnownFolder Name :: VideosLibrary");
    await Enumerate(videosFolder, 1);
}

private async Task Enumerate(StorageFolder folder, Int32 tab)
{
    BasicProperties property = await folder.GetBasicPropertiesAsync();
 
    Debug.WriteLine(GetTab(tab) + "Folder Path :: " + (String.IsNullOrEmpty(folder.Path)? folder.DisplayName : folder.Path));
    IReadOnlyList<StorageFile> files = await folder.GetFilesAsync();
    foreach (var f in files)
    {
        Debug.WriteLine(GetTab(tab + 1) + "File :: " + f.Path);
    }
    IReadOnlyList<StorageFolder> folders = await folder.GetFoldersAsync();
    foreach (var f in folders)
    {
        await Enumerate(f, tab + 1);
    }
}

private String GetTab(Int32 tab)
{
    String strTab = "";
    for (int i = 0; i < tab; ++i)
    {
        strTab += "    ";
    }
    return strTab;
}

執行結果如下

KnownFolder Name :: CameraRoll
    Folder Path :: C:\Users\Ascii\Pictures\Camera Roll
KnownFolder Name :: MusicLibrary
    Folder Path :: Music
        Folder Path :: C:\Users\Ascii\Music\中文歌曲
            File :: C:\Users\Ascii\Music\中文歌曲\ZH-A.mp3
            File :: C:\Users\Ascii\Music\中文歌曲\ZH-B.mp3
            File :: C:\Users\Ascii\Music\中文歌曲\ZH-C.mp3
            Folder Path :: C:\Users\Ascii\Music\中文歌曲\2014 尾牙活動背景音樂
                File :: C:\Users\Ascii\Music\中文歌曲\2014 尾牙活動背景音樂\AA.mp3
                File :: C:\Users\Ascii\Music\中文歌曲\2014 尾牙活動背景音樂\BB.mp3
                File :: C:\Users\Ascii\Music\中文歌曲\2014 尾牙活動背景音樂\CC.mp3
        Folder Path :: C:\Users\Ascii\Music\日語歌曲
            File :: C:\Users\Ascii\Music\日語歌曲\JA-A.mp3
            File :: C:\Users\Ascii\Music\日語歌曲\JA-B.mp3
            File :: C:\Users\Ascii\Music\日語歌曲\JA-C.mp3
        Folder Path :: C:\Users\Ascii\Music\英文歌曲
            File :: C:\Users\Ascii\Music\英文歌曲\EN-A.mp3
            File :: C:\Users\Ascii\Music\英文歌曲\EN-B.mp3
            File :: C:\Users\Ascii\Music\英文歌曲\EN-C.mp3
KnownFolder Name :: PicturesLibrary
    Folder Path :: Pictures
        Folder Path :: C:\Users\Ascii\Pictures\Camera Roll
        Folder Path :: C:\Users\Ascii\Pictures\Windows Simulator
            File :: C:\Users\Ascii\Pictures\Windows Simulator\screenshot_09082014_162416.png
            File :: C:\Users\Ascii\Pictures\Windows Simulator\screenshot_09082014_162424.png
            File :: C:\Users\Ascii\Pictures\Windows Simulator\screenshot_09082014_162431.png
KnownFolder Name :: RemovableDevices
    Folder Path :: Removable Storage Devices
        Folder Path :: F:\
            Folder Path :: F:\USB Folder 1
            Folder Path :: F:\USB Folder 2
            Folder Path :: F:\USB Folder 3
KnownFolder Name :: SavedPictures
    Folder Path :: Pictures
        Folder Path :: C:\Users\Ascii\Pictures\Camera Roll
        Folder Path :: C:\Users\Ascii\Pictures\Windows Simulator
            File :: C:\Users\Ascii\Pictures\Windows Simulator\screenshot_09082014_162416.png
            File :: C:\Users\Ascii\Pictures\Windows Simulator\screenshot_09082014_162424.png
            File :: C:\Users\Ascii\Pictures\Windows Simulator\screenshot_09082014_162431.png
KnownFolder Name :: VideosLibrary
    Folder Path :: Videos
        Folder Path :: C:\Users\Ascii\Videos\教學影片
            File :: C:\Users\Ascii\Videos\教學影片\Android.mp4
            File :: C:\Users\Ascii\Videos\教學影片\Git.avi
            File :: C:\Users\Ascii\Videos\教學影片\Windows Phone.wmv



沒有留言:

張貼留言