Windows Phone 8 mobile phone memory card data

Source: Internet
Author: User
ArticleDirectory
    • 16.1.1 get the memory card folder
    • 16.1.2 obtain the memory card file
    • 16.1.3 instance: Read Memory card information

Mobile phone memory card data in Windows Phone 8 Application Development

The mobile phone memory card is an SD card (full name: Secure Digital Memory Card). You can easily change and install the mobile phone information storage. Third party in Windows Phone 8ProgramOnly the read permission on the memory card, including reading the file directory and file information of the memory card. The APIS for reading memory card information are in Microsoft. phone. in the storage space, the information to read from the memory card must be in the wmappmanifest of the project. the ability to add id_cap_removable_storage to the XML file indicates that the program needs to read the information on the storage card. Otherwise, an exception occurs when calling the API to read the storage card.

16.1.1 get the memory card folder

To read the memory card folder, you must first identify the memory card device. To identify the memory card device, you can use externalstorage. getexternalstoragedevicesasync () to obtain a list of memory card devices asynchronously. The memory card device object is represented by the externalstoragedevice class. The externalstoragedevice class contains two attributes: externalstorageid, which indicates the unique ID string of the extended storage, and rootfolder, which indicates the root directory of the memory card and the default top-level folder. The externalstoragedevice class also contains two asynchronous Methods: The getfileasync method obtains the file information through the file path, and the getfolderasync method obtains the folder information through the folder path.

The externalstoragefolder class indicates the folder class of a memory card, which contains some detailed information about the folder and the folder-related methods. The members of the externalstoragefolder class are shown in table 16.1.

Table 16.1 members of the externalstoragefolder class

name

description

datemodified

file modification time

name

folder name

path

folder path

getfilesasync ()

get all files in this folder

getfolderasync (string name)

obtain information about the folder named in the folder

getfoldersasync ()

get all the folders in the folder

Next, let's take a look atCodeExample:

     Async   Void  Getfolder (){  //  Retrieve the extended memory card list Ienumerable <externalstoragedevice> devicelist = Await  Externalstorage. getexternalstoragedevicesasync ();  //  Traverse the memory card list          Foreach (Externalstoragedevice Device In  Devicelist ){ //  Traverse the root directory of the storage card              Foreach (Externalstoragefolder folder In   Await  Device. rootfolder. getfoldersasync ()){  //  Traverse folders in the memory card                  Foreach (Externalstoragefolder folder2 In   Await  Folder. getfoldersasync ()){  // Obtain the folder information, such as the name folder2.name.  }}}} 

 

16.1.2 obtain the memory card file

The externalstoragefile class represents a storage card file class, which contains some detailed information about the file and the method for opening the file. The externalstoragefile class members are shown in Table 16.2. To read the files on the storage card, you must read the folder and then use the getfilesasync () method of the externalstoragefolder class to obtain all the files in the folder, if you know the file storage path, you can also use the getfileasync (string filepath) method of the externalstoragedevice storage card to obtain the file in the specific path.

Table 16.2 members of the externalstoragefile class

Name

Description

Datemodified

File modification time

Name

Folder name

Path

Folder path

Task <Io. Stream> openforreadasync ()

How to open a folder

The following code shows an example of getting the root directory file of the storage card:

     Async   Void  GetFile (){  // Retrieve the extended memory card list Ienumerable <externalstoragedevice> devicelist = Await  Externalstorage. getexternalstoragedevicesasync ();  //  Traverse the memory card list          Foreach (Externalstoragedevice Device In  Devicelist ){  //  Traverse the files in the root directory of the storage card              Foreach (Externalstoragefile File In   Await Device. rootfolder. getfilesasync ()){  //  Process File Information  }}} 
16.1.3 instance: Read Memory card information

The following is an example of reading the storage card information: Read the information on the storage card of the mobile phone in the mobile phone application, and display the files on the storage card layer by layer according to the folder directory.
Code List 16-1: Read the memory card information (Source code: Chapter 1 \ examples_16_1)

Mainpage. XAML file main code

         <  Grid  X: Name  = "Contentpanel"  Grid. Row  = "1"  Margin = "12, 0, 12, 0"  >              <  Stackpanel  >                  <  Button  Content  = "Getting SD card file information"  X: Name  = "Btgetfile"  Margin  = "0, 0, 0, 80"  Click  = "Btgetfile_click"  />                 <  Textblock  Text  = "Folder list :"  />                  <  ListBox  X: Name  = "Lbfolder"  > </  ListBox  >                  <  Button  Content  = "Open the selected folder"  Click = "Button_click_1"   />                  <  Textblock  Text  = "File list :"  Margin  = "0, 50, 0, 0"  />                  <  ListBox  X: Name  = "Lbfile"  > </  ListBox  >              </ Stackpanel  >          </  Grid  >  

Mainpage. XAML. CS File Code

 Using  System. Collections. Generic;  Using  System. windows;  Using  System. Windows. controls;  Using  Microsoft. Phone. controls;  Using  Microsoft. Phone. storage; Namespace  Sdcarddemo {  Public   Partial   Class  Mainpage: phoneapplicationpage {ienumerable <Externalstoragedevice> devicelist; //  Memory card device list          Public  Mainpage () {initializecomponent ();}  //  Open the root directory of the storage card          Private   Async  Void Btgetfile_click ( Object  Sender, system. Windows. routedeventargs e ){  //  Retrieve the list of memory card Devices Devicelist = Await  Externalstorage. getexternalstoragedevicesasync (); lbfolder. Items. Clear ();  Foreach (Externalstoragedevice Device In  Devicelist ){  //  Add the obtained device root directory information to The ListBox control. Listboxitem item = New  Listboxitem (); item. Content = "  Root directory  " + Device. externalstorageid; item. datacontext = Device. rootfolder; lbfolder. Items. Add (item );}}  //  Open a folder and obtain the folders and files in the folder.          Private   Async   Void Button_click_1 (Object  Sender, system. Windows. routedeventargs e ){  If (Lbfolder. selectedindex =- 1  ) {MessageBox. Show (  "  Select a folder  "  );}  Else  {  //  Obtain the selected folder Listboxitem item = lbfolder. selecteditemAs  Listboxitem; externalstoragefolder externalstoragedevice = Item. datacontext As  Externalstoragefolder;  //  Get folders in the folder Ienumerable <externalstoragefolder> folderlist = Await  Externalstoragedevice. getfoldersasync (); lbfolder. Items. Clear ();  Foreach (Externalstoragefolder folder In Folderlist ){  //  Add the obtained Folder Information to The ListBox control. Listboxitem item2 = New  Listboxitem (); item2.content = "  Folder:  " + Folder. Name; item2.datacontext = Folder; lbfolder. Items. Add (item2 );}  //  Get files in the folder Ienumerable <externalstoragefile> filelist = Await  Externalstoragedevice. getfilesasync (); lbfile. Items. Clear ();  Foreach (Externalstoragefile File In  Filelist ){  //  Add the obtained file information to The ListBox control. Listboxitem item3 = New  Listboxitem (); item3.content = "  File: " + File. Name; item3.datacontext = File; lbfile. Items. Add (item3 );}}}}} 

The program running effect is 16.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.