ArticleDirectory
- 1. Use the isolatedstoragefile class to store files and folders
Learn how to useIsolatedstoragesettings class to save the applicationProgramYou can also useUseIsolatedstoragefileClass storage files and folders. In this note, let's take a look at and learn how to use the isolatedstoragefile class to store files and folders.
1. Use the isolatedstoragefile class to store files and folders
Because I/O operations are involved, we first need to referenceSystem. IoAnd the isolatedstoragefile classSystem. Io. isolatedstorageThe two namespaces. The following example shows how to use the isolatedstoragefile class to store files and folders.
In the example, enter a file in the text box, click Save, and then read and display the saved text. First, we provide our front-endMainpage. XAMLOfCode:
1 <! -- Contentpanel-place other content here -->
2 <Grid X: Name = " Contentpanel " Grid. Row = " 1 " Margin = " 12, 0, 12, 0 " >
3 <Stackpanel>
4 <Grid margin = " 0 15 " Height = " 300 " >
5 <Textbox width = " 450 " Height = " 72 " Verticalalignment = " Top " Name = " Txtwrite " />
6 <Button width = " 200 " Height = " 72 " Content = " Save data " Verticalalignment =" Center " Name = " Btnsave " Click = " Btnsave_click " />
7 </GRID>
8 <Grid Height = " 350 " Margin =" 0 15 " >
9 <Textblock width = " 200 " Height = " 72 " Verticalalignment = " Top " Horizontalalignment = " Center " Fontsize = " 32 " Name = " Txtread " />
10 <Button width = " 200 " Height = " 72 " Content = " Read data " Verticalalignment = " Center " Name = " Btnread " Click = " Btnread_click " />
11 </GRID>
12 </Stackpanel>
13 </GRID>
ThenMainpage. XAML. CSBackground processing program:
1 /// <Summary>
2 /// Write text in the text box to the file
3 /// </Summary>
4 /// <Param name = "sender"> </param>
5 /// <Param name = "E"> </param>
6 Private Void Btnsave_click ( Object Sender, routedeventargs E)
7 {
8 // Obtain the virtual storage of an application
9 Isolatedstoragefile mystore = isolatedstoragefile. getuserstoreforapplication ();
10
11 // Create a new folder in independent storage
12 Mystore. createdirectory ( " Testfolder " );
13
14 // Specify the file path and options
15 Using ( VaR Isofilestream = New Isolatedstoragefilestream ( @" Testfolder \ mytestfile.txt " , Filemode. openorcreate, mystore ))
16 {
17 // Write Data
18 Using ( VaR Isofilewriter = New Streamwriter (isofilestream ))
19 {
20 Isofilewriter. writeline (txtwrite. Text );
21 }
22 }
23 }
24
25 /// <Summary>
26 /// Read written files
27 /// </Summary>
28 /// <Param name = "sender"> </param>
29 /// <Param name = "E"> </param>
30 Private Void Btnread_click ( Object Sender, routedeventargs E)
31 {
32 // Obtain the virtual storage of an application
33 Isolatedstoragefile mystore = isolatedstoragefile. getuserstoreforapplication ();
34 Try
35 {
36 // Reads a specified file from a specified directory.
37 Using ( VaR Isofilestream = New Isolatedstoragefilestream (@" Testfolder \ mytestfile.txt " , Filemode. Open, mystore ))
38 {
39 // Read data
40 Using ( VaR Isofilereader = New Streamreader (isofilestream ))
41 {
42 Txtread. Text = isofilereader. Readline ();
43 }
44 }
45 }
46 Catch
47 {
48 // Exception Handling
49 Txtread. Text = " Please create files and folders in advance " ;
50 }
51 }
Compile and run the program:
We can see that a folder and TXT file have been created successfully, and data is written into the TXT file. But do we actually create a file like this in the mobile phone storage activation? Microsoft's storage control in Windows Phone is very strict. We cannot directly view the files we created in the independent storage, but we canUse: The independent storage resource manager can list, copy, and replace files and directories in the independent storage.Next, we will copy the file we created to our computer.
2.use the independent storage resource manager (isw.l.exe) to copy the files created in the independent storage to the computer
Depending on the operating system, the installation location of the independent storage resource manager is as follows:
First, weOpen the simulator and make sure that our previous application has been deployed in the simulator and the file has been created in the standalone storage.Then, open the command line and enter the installation directory of the independent storage resource manager. For example, on my computer:
Then obtainProduct guidBecause we need to use it in the following commandThe wpappmanifest. xml file in the properties folderAppElementProductidAttribute Value, Select copy.Then, enter:
The productid attribute value of the isetool.exe ts XD application "path on the computer"
Here I am doing this:
Press enter, and the command is successful as follows:
Open the directory we copied to and we can see thatSuccessfulCopy the created file from Windows Phone to the local device:
Download with a click: sample source code
References:
Http://msdn.microsoft.com/zh-cn/library/ff626519 (V = vs.92). aspx (important)
Http://msdn.microsoft.com/zh-cn/library/hh286408 (V = vs.92). aspx
Http://msdn.microsoft.com/zh-cn/library/ff769544 (V = vs.92). aspx (important)
Author: Sunny pig
Source: Http://www.cnblogs.com/IPrograming
The copyright of this article is shared by the author and the blog Park. You are welcome to repost this article. However, you must retain this statement without the author's consent and provide a clear link to the original article on the article page. Otherwise, you will be held legally liable.
Windows Phone developer group: 79339880, you are welcome to join in discussions and make progress together.