6.2.4 application file URI Scheme
In the above method, we obtain the folder object and file object through the three folder objects in the app and directory, in this section, we will describe a new method for getting file objects. This method is obtained through the URI address. The path of the hosts file is as follows:
C: \ data \ Users \ defapps \ appdata \ Local \ packages \ 6c522da7-81ed-4463-b58a-584c89af115e_thbaz9fn8knhr \ localstate \ testfile.txt
The format of the storage path corresponding to the three root directory folders is as follows:
(1) Save path format of the localfolder Folder:
% USERPROFILE % \ appdata \ Local \ packages \ {packageid} \ localstate
(2) Save path format of roamingfolder Folder:
% USERPROFILE % \ appdata \ Local \ packages \ {packageid} \ roamingstate
(3) Saving path format of the temporaryfolder Folder:
% USERPROFILE % \ appdata \ Local \ packages \ {packageid} \ tempstate
The obtained path cannot be used as the file access path. The file access path must use the MS-appdata URI scheme of the local folder. The localfolder folder corresponds to "MS-appdata: // local/", and the roamingfolder folder corresponds to "MS-appdata: // roaming /", the temporaryfolder folder corresponds to "MS-appdata: // temp /". We can use the static method getfilefromapplicationuriasync of the storagefile class to read files based on the URI. The following sample code uses the URI scheme of MS-appdata to obtain the appconfigsettings. xml file in the localfolder folder.
Var file = await storagefile. getfilefromapplicationuriasync (New uri ("MS-appdata: // local/appconfigsettings. xml "));
In this scheme of file access through Uri, we also need to pay attention to one thing, that is, the path of the new file and folder cannot exceed 185 characters after the last slash of the URI scheme name.
The following example shows how to read a file through URI: This example shows how to obtain the absolute path of a file after it is created, and read the file through the URI scheme.
Code List 6-6:Read files through uri (source code: Chapter 6th \ examples_6_6)
Mainpage. the main code of the XAML file is struct <stackpanel> <textbox header = "File Information:" X: Name = "info" textwrapping = "Wrap"> </textbox> <button X: name = "bt_save" content = "Create File" width = "370" Click = "bt_save_click"> </button> <button X: name = "bt_read" content = "read files through Uri" width = "370" Click = "bt_read_click"> </button> </stackpanel>
Mainpage. XAML. main Code of the CS file: // file name private string filename = "testfile.txt"; // create the file private async void bt_save_click (Object sender, routedeventargs e) {If (info. text! = "") {// Obtain the root folder of the local folder, istoragefolder applicationfolder = applicationdata. current. localfolder; // create a file in the folder. If the file exists, replace istoragefile storagefile = await applicationfolder. createfileasync (filename, creationcollisionoption. openifexists); // use the fileio class to write string information to the file await fileio. writetextasync (storagefile, info. text); await new messagedialog ("saved successfully, file path:" + storagefile. path ). showasync ();} else {await new messagedialog ("content cannot be blank "). showasync () ;}/// read the file private async void bt_read_click (Object sender, routedeventargs e) {// read the file text string text; try {// obtain the local file var storagefile = await storagefile through Uri. getfilefromapplicationuriasync (New uri ("MS-appdata: // local/" + filename); // open the file to obtain the data stream irandomaccessstream accessstream = await storagefile. openreadasync (); // use streamreader to read the file content. You need to convert the irandomaccessstream object to the stream object to initialize the streamreader object using (streamreader = new streamreader (accessstream. asstreamforread (INT) accessstream. size) {text = streamreader. readtoend () ;}} catch (exception exce) {text = "file read error:" + exce. message;} await new messagedialog (text ). showasync ();}
This article comes from the introduction to Windows Phone 8.1 Application Development
Source code download: http://vdisk.weibo.com/s/zt_pyrfNHb99O
Welcome to my weibo @ WP Lin Zheng public account: Wp Development (No.: wpkaifa)
Wp8.1 technical exchange group: 372552293
[Wp8.1 (runtime)] URI scheme of the application file