Windows Phone Silverlight 8.1 App Write Access to SD Card

With the arrival of Windows Phone 8.1, it is now able to have write access to the SD card of a Windows Phone 8.1 device, in contrast to Windows Phone 8 which had read-only access to SD card. Still, there are a couple things that you need to know before you try it at home:

  • You need to specify the removableStorage capability
  • You need to register file types to hanlde

Further reading: MSDN

First of all, when you upgrade an existing Windows Phone 8 project to Windows Phone 8.1 from Visual Studio 2013, you end up with a Windows Phone Silverlight 8.1 project. Moreover, your project will have two manifest files, instead of one. There is the Windows Phone 8  WMAppManifest.xml file and the Windows Phone 8.1 Package.appxmanifest. If you create a new Windows Phone 8.1 project it is better to use Windows Runtime (WinRT) so as to be able to create Universal Apps both for Windows Phone and Windows Store.

Further reading: About Windows Phone 8.1 and universal apps, WP 8.1 RT vs Silverlight, Upgrade Windows Phone 8 project

You will need to be check the Removable Storage capability under the Capabilities tab in Package.appxmanifest. If you accidentally select only the ID_CAP_REMOVABLE_STORAGE from WMAppManifest.xml you will end up with the following error message when trying to use KnownFolders:

“Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))”

File type associations need to be declared in Package.appxmanifest. From the UI, choose Declerations tab and then select File Type Associations from Available Declerations. In Visual Studio 2013, it is not possible to select File Type Associations from the UI, when your project is Windows Phone Silverlight 8.1. Instead you need to manually edit the xml file (View Code F7) and add Extension under the Applications section:

<Applications>
  ...
    <Extensions>
       <Extension Category="windows.fileTypeAssociation">
           <FileTypeAssociation Name=".bak">
             <DisplayName>Backup File</DisplayName>
             <SupportedFileTypes>
                 <FileType ContentType="audio/bak">.bak</FileType>
             </SupportedFileTypes>
           </FileTypeAssociation>
        </Extension>
    </Extensions>
  ...
</Applications>
 

Further reading: File Type Associations in Windows Phone 8.1

Hopefully after these actions you are ready to write your own file in the SD card and make your app able to keep backups/logs:

var sdCard = KnownFolders.RemovableDevices;

// Get the root folder for the external device.
var sdCardRoot = (await sdCard.GetFoldersAsync()).OrderBy(eachDir => eachDir.DisplayName).FirstOrDefault();
if (sdCard != null && sdCardRoot != null)
{

// An SD card is present and the sdCard variable now contains a reference to it.

String backupFilename = “backup.bak”;

// Create the backup file or open it if it already exists
var backupFile = await sdCardRoot.CreateFileAsync(sdCardRoot.Path + “\\” + backupFilename, CreationCollisionOption.OpenIfExists);

if (backupFile != null)
{

using (var textStream = await backupFile.OpenAsync(FileAccessMode.ReadWrite))
{

using (DataWriter textWriter = new DataWriter(textStream))
{

textWriter.WriteString(“Something to write into my backup file!”);
await textWriter.StoreAsync();

}

}

}

}
else
{
// No SD card is present.
}