Search code examples
sd-cardwindows-phone-8.1

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:

MSDN: Windows Phone 8.1 Access SD Card

You specify the removableStorage capability and register for desired file extensions you need to handle, and you are able to view SD card's files and folders.

My question is how you create a new file inside the SD card, if it does not exist? I have managed to do it with the following after looking into the private member m_internalFolderPath of ExternalStorageFolder:

File.Open("D:\\test.txt", FileMode.OpenOrCreate);

But is it OK to use "D:" as the root path for SD card? I can't find any reference this online. Is letter "D:" device/vendor specific or should I trust it as the letter for SD card?


Solution

  • Creating file via a path beginning with D:

    File.Create(@"D:\test.txt");
    

    will work in most cases, but it's not said anywhere that OS will assign letter D for the SD card (thought in fact I haven't seen other). But to be sure, you can always access SD card via StorageFolder:

    StorageFolder SDDevice = Windows.Storage.KnownFolders.RemovableDevices;
    StorageFolder sdCard = (await SDDevice.GetFoldersAsync()).FirstOrDefault();
    await sdCard.CreateFileAsync("test.txt", CreationCollisionOption.ReplaceExisting)
    

    To make those above work you need to have declared Capabilities and FileTypeAssociations in package.appxmanifest file.

    There is a little issue with VS and Windows Phone 8.1 Silverlight - you will need to add FileTypeAssociations manually.