Windows Phone development (28): isolated storage B

Source: Internet
Author: User

In the previous section, we talked about directory operations. This section continues to show you how to read and write files.

First of all, let's talk about the problem. Many of my friends may be confused. In fact, this learning attitude is very bad. If you are interested in Windows Phone development, if you really want to learn, you should calm down and learn.

If you do not like Windows Phone development, you do not have to worry about it. You can choose iOS, Android, or other platforms.
As long as you choose, you should believe what you choose. Remember to say this in one sentence: select what you love and what you choose. Although this is not an appropriate analogy, but it means similar.

In fact, in the end, it is not how difficult programming is to learn, but the fundamental problem of a lot of halfway work is the learning attitude. We should not say that our elders, for example, in the past 60 s and 70 s, how can people lag behind and cannot keep up with the times? Right, in terms of knowledge accumulation and skills, our elders really cannot keep up with the times. However, they have an advantage that none of our young people after the 80 s and after the 90 s have, that is, the spirit of perseverance and hardship. This is a fact, I hope all of you will face up to this point. If you are not afraid of many shortcomings, you will not be afraid to face your shortcomings.

As young people, we should also have the courage to "dare to face the gloomy life, dare to face up to the dripping blood.

You don't have to worry about the future of Windows Phone As Long As You Like It. the future of net should not be intimidated by some news and comments. As a rational subject, we should also distinguish authenticity. Many News and comments are misleading us.
It doesn't matter whether Microsoft is slow or slow. The market is very small at present, but you know, there must be its value. It giants are competing fiercely. as developers, we only need to study in a down-to-earth manner.
Recently, employees of Google and Oracle are studying legal knowledge, while Microsoft's staff are learning marketing. From these phenomena, we know that both open source and closed source have their own advantages and disadvantages, it is king to strike a balance between the two.

Well, let's talk about this nonsense. Today's content is very simple, so I just spoke a lot about it. The purpose is to tell everyone that WP developers should not be impetuous. As long as you can train WP development skills out of the box, even if the market is small, you can make a lot of money. Ma Ning is a success story.

Isolate the read and write operations of stored files from other files. there is no difference in File Reading and Writing in net development. We only use isolatedstoragefilestream on WP, instead of the traditional filestream. To put it bluntly, we just changed the class name and all the operations were the same, as for whether you believe it or not, I believe it.

Now, I use an example to demonstrate how easy it is to read and write files.
Create a new project and put a text box on the home page to enter the content of the file to be written, put two buttons, one for write operation, one for read operation, and the other for textblock, displays the content read from a file. The XAML layout is as follows.

 

<Phone: phoneapplicationpage <br/> X: class = "phoneapp1.mainpage" <br/> xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" <br/> xmlns: X = "http://schemas.microsoft.com/winfx/2006/xaml" <br/> xmlns: Phone = "CLR-namespace: Microsoft. phone. controls; Assembly = Microsoft. phone "<br/> xmlns: shell =" CLR-namespace: Microsoft. phone. shell; Assembly = Microsoft. phone "<br/> xmlns: D =" http://schemas.microsoft.com/expression/blend/2008 "<br/> xmlns: MC =" http://schemas.openxmlformats.org/markup-compatibility/2006 "<br/> MC: ignorable =" D "D: designwidth = "480" D: designheight = "768" <br/> fontfamily = "{staticresource phonefontfamilynormal}" <br/> fontsize = "{staticresource quota}" <br/> foreground = "{staticresource phoneforegroundbrush} "<br/> supportedorientations =" portrait "orientation =" portrait "<br/> shell: systemtray. isvisible = "true"> <br/> <stackpanel margin = "" orientation = "vertical"> <br/> <textbox name = "txtcontent "horizontalalignment =" stretch "Height =" 185 "scrollviewer. verticalscrollbarvisibility = "Auto" textwrapping = "Wrap"/> <br/> <button name = "btnwrite" horizontalalignment = "stretch" Height = "Auto" content = "write content file "Click =" btnwrite_click "/> <br/> </stackpanel> <br/> <stackpanel margin =" 0,25 "orientation =" vertical "> <br/> <button horizontalalignment = "stretch" content = "read from a file" name = "btnread" Click = "btnread_click"/> <br/> <textblock name = "txtdisplay" horizontalalignment = "stretch ""Height =" 358 "scrollviewer. verticalscrollbarvisibility = "Auto" textwrapping = "Wrap" fontsize = "35"/> <br/> </stackpanel> </P> <p> </phone: phoneapplicationpage>

The background C # is as follows.

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. net; <br/> using system. windows; <br/> using system. windows. controls; <br/> using system. windows. documents; <br/> using system. windows. input; <br/> using system. windows. media; <br/> using system. windows. media. animation; <br/> using system. windows. shapes; <br/> using Microsoft. phone. controls; <br/> // introduce this namespace <br/> using system. io; <br/> using system. io. isolatedstorage; </P> <p> namespace phoneapp1 <br/>{< br/> Public partial class mainpage: phoneapplicationpage <br/>{< br/> // constant <br/> const string mydir = "mydata"; <br/> const string testfilename = "file "; </P> <p> // constructor <br/> Public mainpage () <br/>{< br/> initializecomponent (); <br/> This. loaded + = (sender, e) => <br/>{< br/> using (var iso = isolatedstoragefile. getuserstoreforapplication () <br/>{< br/> If (ISO. directoryexists (mydir) = false) <br/>{< br/> ISO. createdirectory (mydir); <br/>}< br/>}; <br/>}</P> <p> private void btnwrite_click (Object sender, routedeventargs e) <br/>{< br/> using (var iso = isolatedstoragefile. getuserstoreforapplication () <br/>{< br/> using (VAR sr = ISO. createfile (mydir + "\" + testfilename) <br/>{< br/> streamwriter Sw = new streamwriter (SR); <br/> SW. write (txtcontent. text); <br/> SW. close (); <br/> SW. dispose (); <br/>}</P> <p> private void btnread_click (Object sender, routedeventargs E) <br/>{< br/> using (var iso = isolatedstoragefile. getuserstoreforapplication () <br/>{< br/> var sr = ISO. openfile (mydir + "\" + testfilename, filemode. open, fileaccess. read); <br/> streamreader reader = new streamreader (SR); <br/> string info = reader. readtoend (); <br/> reader. close (); <br/> reader. dispose (); <br/> Sr. close (); <br/> Sr. dispose (); <br/> txtdisplay. TEXT = Info; <br/>}< br/>}

 

The aboveCodeI think you can understand it. Yes, it's that simple. Now you believe it.

Come on, let's see the results after the operation.

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.