Read-write method rollup for SD card files in Windows Phone8.1

Source: Internet
Author: User

At first I wanted to read a file from an SD card in two ways:

1. Through the file selector Fileopenpicker, to layer to the phone to find the files you need to read, and then click directly read the display content

2. Read the file directly to the SD card

The first method of logic is somewhat complex, designed to apply temporary suspend and resume, this blog does not delve into this method

The second approach, the logic is relatively simple. Just get to the SD card object, traverse the file inside or specify a folder directly, then

To read the contents of a file or a folder.


For the above two methods, as a beginner I have recently tried several times, I really feel learned a lot. Many times blog, Post bar, forum, in Baidu

Also 1100 degrees, the following summary I see a good few blog about this, recommend to everyone:

First of all, Microsoft's MSDN is the most comprehensive introduction document, although I do not like it too much written explanation, every time it is difficult to understand a hard to say more difficult

Understandable, it's easy for readers to be intimidating. However, it cannot be denied that it is indeed a good tool for learning!

Information on MSDN (this chapter focuses on knownfolders): Windows.storage Namespace

Programming Small Dream: WP8.1 Development: SD card Read and write specific types of files

Xiao Qiang (externalstoragedevice no longer supported) : The SD card read---on Windows phone 8 reads the audio file and sets the ringtone to the phone

Lipan (slightly older, much less supported): Windows Phone 8 Learning Note (4) app startup-file association and SD card access


Here are some of my personal sentiment and summary, we actually read the above blog should understand the eight or nine points, there is a need for my can make a look.

First of all, to read the files in the SD card, but to have permission, you can not give no reason to read or write. So before you actually write the code, you have to do

Some preparatory work.

First: Let the phone know that you want to access the files in SD, you present this requirement so that it can be specified that you are able to use this feature.


Of course you can also open the Package.appxmanifest in XML format, and then add a line of code inside yourself:



Second: After you have access to the files in the SD card permissions, but also to declare which format to access the file (add a declaration, declare the file type association specified)


Of course you can still write your own code to open the above file in XML form:


OK, the preparation is ready, the following will be free to write code:

The first step:

How do I get an SD card? code before adding:

Explanation for the KnownFolders class in MSDN: KnownFolders class

an explanation of the Storagefolder class in MSDN: Storagefolder Class

Step Two:

Get the SD card folder and create new folders and files and write to the content:

(NewFolder is a defined global Storagefolder object; TextBlock is the Name property of the TextBlock label for a XAML page)

var devices = windows.storage.knownfolders.removabledevices;//returns storagefolder type var sdunion = await devices. Getfoldersasync ();//Gets a list of the top-level subfolders of the current folder (the SD card can be viewed as a folder) if (Sdunion.count = = 0) return; Storagefolder Firstcard = sdunion[0];//You can think of the above code, get the SD card inserted into the phone collection (in fact, is usually an SD card, but the look is still to do), and then get the set of the first SD card (Sdcards[0]) Creates a new subfolder within the current folder and specifies the action that should be taken if a folder with the same name exists in the current folder. Returns Storagefolder type NewFolder = await Firstcard.createfolderasync (" Test ", creationcollisionoption.openifexists);//Create a new file in the current folder, and specifies what to do if the file name duplicates a file already in the current folder. Returns storagefile type Storagefile NewFile = await Newfolder.createfileasync ("Demo.txt", creationcollisionoption.replaceexisting); await Fileio.writetextasync (NewFile, TextBlock. Text, Windows.Storage.Streams.UnicodeEncoding.Utf8);

The reading process is almost the same, and the last two lines of the code above are replaced with the code read:

StorageFile NewFile = await Newfolder.getfileasync ("test.txt"); TextBlock. Text = await fileio.readtextasync (newFile);

of course, not every time we go in, we're going to create a new file and determine if it exists, and use openifexists to get it if it already exists .

This file object, this method is too trickery.

Let's not think about it, but just do it under the premise of creating a new file. To do what you want, you have to look at the MSDN related

Documentation of the data.

MSDN Getfolderasync (String name) method: Getfolderasync (String name) method

When we need to obtain a file that we know the name of, directly through the Storagefolder class of the Getfolderasync () method to obtain

NewFolder = await Firstcard.getfolderasync ("Test");


Read-write method rollup for SD card files in Windows Phone8.1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.