Reputation: 43
I'd like to ask, if anyone knows a good method to get all filenames with their absolute path from SD card folder and put them into list??? I am programming for Windows Phone 8.
Example output:
String[] listOfItems = { "SdcardRoot/Music/a.mp3",
"SdcardRoot/Music/ACDC/b.mp3",
"SdcardRoot/Music/someartist/somealbum/somefile.someextention" };
Thank You for your time.
Upvotes: 2
Views: 2794
Reputation: 1917
Here is a step by step guide to achieve what you want. If something remains unclear you also have this one from msdn.
You will certainly end up with a code like this one :
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");
}
}
The file
variable located inside files
loop is of type ExternalStorageFile. The Path
property seems to be the one you need :
This path is relative to the root folder of the SD card.
Finally, do not forget to add the ID_CAP_REMOVABLE_STORAGE capability in the WMAppManifest.xml of your application and to register file association :
We need to declare additional capability to register a certain file extension with the application. To ensure that we can read files of a certain type, we need to register file association via extensions in the Application Manifest file. For this, we need to open the WMAppManifest.xml file as code and make the following changes.
Upvotes: 1