Windows Phone 8文件SD卡文件列表

时间:2013-10-26 23:22:48

标签: c# windows-phone-8

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

示例输出:

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

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

这是一个step by step guide来实现你想要的。如果有些事情仍然不清楚,你也有one from 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");
        }
    }

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

  

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

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

  

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

相关问题