Reputation: 1060
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?
Upvotes: 0
Views: 571
Reputation: 153
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;
}
Upvotes: 1