Windows Phone 8 文件列表的 SD 卡文件

本文关键字:文件 SD 列表 Windows Phone | 更新日期: 2023-09-27 18:37:08

我想

问一下,如果有人知道一种好方法可以从SD卡文件夹中获取所有文件名及其绝对路径并将它们放入列表中??? 我正在为 Windows Phone 8 编程。

示例输出:

String[] listOfItems = { "SdcardRoot/Music/a.mp3", 
                         "SdcardRoot/Music/ACDC/b.mp3", 
                         "SdcardRoot/Music/someartist/somealbum/somefile.someextention" };

谢谢你的时间。

Windows Phone 8 文件列表的 SD 卡文件

这是实现您想要的分步指南。如果仍然不清楚,您也可以从msdn获得此内容。

你肯定会得到这样的代码:

    private async Task ListSDCardFileContents()
    {
        List<string> listOfItems = new List<string>();
        // List the first /default SD Card whih is on the device. Since we know Windows Phone devices only support one SD card, this should get us the SD card on the phone.
        ExternalStorageDevice sdCard = (await ExternalStorage.GetExternalStorageDevicesAsync()).FirstOrDefault();
        if (sdCard != null)
        {
            // Get the root folder on the SD card.
            ExternalStorageFolder sdrootFolder = sdCard.RootFolder;
            if (sdrootFolder != null)
            {
                // List all the files on the root folder.
                var files = await sdrootFolder.GetFilesAsync();
                if (files != null)
                {
                    foreach (ExternalStorageFile file in files)
                    {
                        listOfItems.Add(file.Path);
                    }
                }
            }
            else
            {
                MessageBox.Show("Failed to get root folder on SD card");
            }
        }
        else
        {
            MessageBox.Show("SD Card not found on device");
        }
    }

位于循环files file变量的类型为 ExternalStorageFile。Path属性似乎是您需要的属性:

此路径相对于 SD 卡的根文件夹。

最后,不要忘记在应用程序的 WMAppManifest.xml 中添加ID_CAP_REMOVABLE_STORAGE功能注册文件关联

我们需要声明其他功能来向应用程序注册某个文件扩展名。 为了确保我们可以读取某种类型的文件,我们需要 通过应用程序清单中的扩展注册文件关联 文件。为此,我们需要打开 WMAppManifest.xml 文件作为代码和 进行以下更改。