0

I am very much confused with WP 8.1 (runtime) SD card access. I am trying to create a folder in WP 8.1 (Runtime) Sd Card, but unable to do so. I am following MSDN tutorial to access sd card in WinRT. I need SD card access in order to store my App's backup & log folders. By following this tutorial I configured sd card path in emulator and I am using below code to access that path.

 private async Task<string> AsyncExternalStoragePath()
 {
   // Get the logical root folder for all external storage devices.
   Windows.Storage.StorageFolder externalDevices = Windows.Storage.KnownFolders.RemovableDevices;

   // Get the first child folder, which represents the SD card.
   StorageFolder sdCard = (await externalDevices.GetFoldersAsync().AsTask().ConfigureAwait(false)).FirstOrDefault();


 }

externalDevices.Path returns empty string instead of returning the path which I configured. I enabled RemovableStorage under Capabilities and added FileTypeAssociations. But honestly this FileTypeAssociations are very much confusing to me. I read many articles, but I am not yet clarified fully.

Basically I want two folders and one file under my app name folder.

AppnameFolder -> BackupFolder, LogFolder, Infile

Under each folder

Backup -> To keep last five backup files (.db)
Log -> To create log files per day basis (.txt)
inifile -> To enable logging

If I know how to create a custom folder (app name folder) in sd card, I would create sub-folders (backup, log) easily. But right now I am stuck in getting the root path.

Edit: Deadlock problem got solved by adding ConfigureAwait(false) in async call as suggested in comment, but sdcard path is still null, How can I get path and create folder there?

subha
  • 1,050
  • 2
  • 15
  • 42

1 Answers1

1

With "externalDevices" you are querying all the storage devices in your phone. Then trying to find its path.

You should find the path of the root folder of the sdcard, not the path of the sdcard itself.

"sdCard.path" will work.

Edit after Subha's comment:
Assuming the Removable Storage is declared in appxmanifest and the emulator has the emulated sdcard inserted (as described is the tutorials; did Storage Sense give a popup?) the following should work.
Note that I am not using task.

StorageFolder sdcard;

public async void read_sdcard_button_click(object sender, RoutedEventArgs e)
{
    sdcard = (await KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();
}

private void some_other_method()
{
    textbox.Text = sdcard.path;
}
mcb
  • 153
  • 6
  • I have configured sd card path in emulator, but it returns sdCard as null, also how to get root folder of the sdcard? – subha Jul 27 '15 at 12:43
  • Edited my answer. Can you check/try this? – mcb Jul 27 '15 at 13:23
  • My code is same as yours, instead of directly accessing RemovableDevices.GetFolderAsync(), I am using externalDevices variable, that's it right? I added ConfigureAwait to fix deadlock. – subha Jul 27 '15 at 13:28
  • Using *externalDevices* should not be a problem. (Personally I don't use extra variables if not really needed.) The only difference in our code is the "run as task bit". Should not be problem though. – mcb Jul 27 '15 at 13:39
  • Just did some testing. Reverted the emulator to the default checkpoint (so without the sdcard), started my app and it gave a null-reference to the sdcard (as you described in your post). Inserted the sdcard (StorageSense app popped up "_new sdcard found_") and ran my app again. Now the sdcard is found and the folders and files can be queried. Double check if StorageSense can actually detect the sdcard. It looks like a problem with the sdcard in the emulator rather the code in your app. – mcb Jul 27 '15 at 13:57