Windows Phone development (28): isolated storage B-: http://blog.csdn.net/tcjiaan/article/details/7436959

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.

 

[HTML]View plaincopyprint?

  1. <Phone: phoneapplicationpage
  2. X: class = "phoneapp1.mainpage"
  3. Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
  5. Xmlns: Phone = "CLR-namespace: Microsoft. Phone. controls; Assembly = Microsoft. Phone"
  6. Xmlns: shell = "CLR-namespace: Microsoft. Phone. Shell; Assembly = Microsoft. Phone"
  7. Xmlns: D = "http://schemas.microsoft.com/expression/blend/2008"
  8. Xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. MC: ignorable = "D" D: designwidth = "480" D: designheight = "768"
  10. Fontfamily = "{staticresource phonefontfamilynormal }"
  11. Fontsize = "{staticresource phonefontsizenormal }"
  12. Foreground = "{staticresource phoneforegroundbrush }"
  13. Supportedorientations = "portrait" orientation = "portrait"
  14. Shell: systemtray. isvisible = "true">
  15. <Stackpanel>
  16. <Stackpanel margin = "0, 25" orientation = "vertical">
  17. <Textbox name = "txtcontent" horizontalalignment = "stretch" Height = "185" scrollviewer. verticalscrollbarvisibility = "Auto" textwrapping = "Wrap"/>
  18. <Button name = "btnwrite" horizontalalignment = "stretch" Height = "Auto" content = "write content to file" Click = "btnwrite_click"/>
  19. </Stackpanel>
  20. <Stackpanel margin = "0, 25" orientation = "vertical">
  21. <Button horizontalalignment = "stretch" content = "read from file" name = "btnread" Click = "btnread_click"/>
  22. <Textblock name = "txtdisplay" horizontalalignment = "stretch" Height = "358" scrollviewer. verticalscrollbarvisibility = "Auto" textwrapping = "Wrap" fontsize = "35"/>
  23. </Stackpanel>
  24. </Stackpanel>
  25. </Phone: phoneapplicationpage>

 

The background C # is as follows.

[CSHARP]View plaincopyprint?

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. LINQ;
  4. Using system. net;
  5. Using system. windows;
  6. Using system. Windows. controls;
  7. Using system. Windows. documents;
  8. Using system. Windows. input;
  9. Using system. Windows. Media;
  10. Using system. Windows. Media. animation;
  11. Using system. Windows. shapes;
  12. Using Microsoft. Phone. controls;
  13. // Introduce this namespace
  14. Using system. IO;
  15. Using system. Io. isolatedstorage;
  16. Namespace phoneapp1
  17. {
  18. Public partial class mainpage: phoneapplicationpage
  19. {
  20. // Constant
  21. Const string mydir = "mydata ";
  22. Const string testfilename = "file ";
  23. // Constructor
  24. Public mainpage ()
  25. {
  26. Initializecomponent ();
  27. This. Loaded + = (sender, e) =>
  28. {
  29. Using (var iso = isolatedstoragefile. getuserstoreforapplication ())
  30. {
  31. If (ISO. directoryexists (mydir) = false)
  32. {
  33. ISO. createdirectory (mydir );
  34. }
  35. }
  36. };
  37. }
  38. Private void btnwrite_click (Object sender, routedeventargs E)
  39. {
  40. Using (var iso = isolatedstoragefile. getuserstoreforapplication ())
  41. {
  42. Using (VAR sr = ISO. createfile (mydir + "\" + testfilename ))
  43. {
  44. Streamwriter Sw = new streamwriter (SR );
  45. Sw. Write (txtcontent. Text );
  46. Sw. Close ();
  47. Sw. Dispose ();
  48. }
  49. }
  50. }
  51. Private void btnread_click (Object sender, routedeventargs E)
  52. {
  53. Using (var iso = isolatedstoragefile. getuserstoreforapplication ())
  54. {
  55. VaR sr = ISO. openfile (mydir + "\" + testfilename, filemode. Open, fileaccess. Read );
  56. Streamreader reader = new streamreader (SR );
  57. String info = reader. readtoend ();
  58. Reader. Close ();
  59. Reader. Dispose ();
  60. Sr. Close ();
  61. Sr. Dispose ();
  62. Txtdisplay. Text = Info;
  63. }
  64. }
  65. }
  66. }

 

The code above can be easily understood. Yes, it is so 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.