Cross-Windows Phone 8 and Windows 8 (winrt) simple File System Access

Source: Internet
Author: User

To tell the truth, I was a little disappointed when I first saw the Windows Phone 8 SDK, because it had pitted Nokia and abandoned the new platform for countless Windows Phone 7 users and was not fully connected to Windows 8 winrt, although Windows Phone 8 runs the Windows 8 kernel, there are not many intersections between Windows 8 and Windows 8 winrt, while Windows 8 Development is like a new start. Bill Gates also said a while ago that Windows Phone and windows will eventually become an operating system (refer to link 1 and 2). I hope this dream will be realized soon. If winrt is Microsoft's final dream, windows 8 is already at the forefront (but winrt still needs to be developed to make it more mature and powerful), while Windows Phone 8 can only say sorry that I still have some ways to go.

 

Okay, I spoke a lot. In the following text, I will briefly talk about the intersection of Windows Phone 8 and Windows 8 (winrt): file system.

Note: Windows Phone 8's winmd can be found in "C: \ Program Files (x86) \ Windows Phone kits \ 8.0 \ Windows metadata \ windows. winmd ", while the winrt file system is independent in a winmd file:" C: \ windows \ system32 \ winmetadata \ windows. storage. winmd"

 

Windows Phone 8's file system can be considered as a subset of winrt, which has a common name and namespace. Windows Phone 8 will cut off some types of execution, in addition, there may be more restrictions on executed types than winrt. For example, Windows Phone 8 does not have the pathio and fileio types, or windows. storage. in the pickers namespace, only fileopenpicker in Windows Phone 8 is a folderpicker without winrt. Obviously, Windows Phone 8 has more restrictions, even all fileopenpicker.

 

However, their core elements are the same. For example, istorageitem represents a file system object:

There are also new stream-related types:

These are almost the same on both platforms.

 

OK. Now, write a simple applet that uses this universal File System: Output all the content in a storagefolder. For the sake of simplicity, we use a textblock as the output on the interface, and then add a loaded event:

<Textblock name = "TBL" loaded = "tbl_loaded_1"/>

 

Note that Windows Phone 8 and Windows 8 (winrt) both use XAML on the interface elements, but the XAML of Windows Phone 8 follows the Windows Phone 7 naming space dominated by system. Windows. Winrt's XAML is centered on Windows. UI. XAML namespaces.

 

Note that you also need to reference the required namespaces (obviously, the namespaces developed by Windows applications are a little messy in the future, remember that the namespaces started with system are usually. net type. The namespace starting with Windows is usually related to the winrt framework ):

Using Windows. ApplicationModel; // package type

Using Windows. Storage; // storagefolder AND OTHER TYPES

Using system. Threading. tasks; // Task Type

Using system. Text; // stringbuilder type

 

Then we start to define the main method. We use the simplest storagefolder getfolders and getfiles methods to recursively output all the content in the folder (note, windows Phone 8 and winrt are both asynchronously driven ):

// Asynchronously outputs Folder Information

Static async task showcontentasync (stringbuilder Sb, storagefolder Dir, int indent)

{

// Indent related variables

VaR currentindentstr = new string ('', indent );

VaR nextindent = indent + 4;

VaR nextindentstr = new string ('', nextindent );

 

// Output the name of the current folder

SB. append (currentindentstr );

SB. appendline (">" + dir. Name );

 

// Output and recursive subfolders

Foreach (VAR folder in await dir. getfoldersasync ())

Await showcontentasync (SB, folder, nextindent );

// Output the sub-File

Foreach (var file in await dir. getfilesasync ())

SB. appendline (nextindentstr + file. Name );

}

 

Finally, add the call code to the loaded event of textblock. Note that you need to change it to The async method. For the folders to be explored, select the installation folder of the current application (through windows. the installedlocation attribute of the package type in the ApplicationModel namespace), you can also select other default authorized access folders, such as the local data folder of the application (through windows. applicationdata in the storage namespace. current. localfolder attribute ):

// Loaded event

Private async void tbl_loaded_1 (Object sender, routedeventargs E)

{

// Create a stringbuilder

VaR sb = new stringbuilder ();

// Take the installation folder of the application as the parameter and run it

Await showcontentasync (SB, package. Current. installedlocation, 0 );

// Set the result to the textblock on the Interface

TBL. Text = sb. tostring ();

}

 

Note that although the development code is the same, the applications of different operating systems must be compiled by the corresponding compiler. We still develop two programs for the two systems, but the code is the same. Visual Studio 2012 express for Windows 8 and Visual Studio express for Windows Phone are still required to create and compile the project respectively. (Of course, if Bill Gates is implemented, it will not be needed .)

 

Running result in Windows Phone 8:

 

Running results on Windows 8 (winrt:

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.