zoukankan      html  css  js  c++  java
  • Windows Phone开发(28):隔离存储B 转:http://blog.csdn.net/tcjiaan/article/details/7436959

    上一节我们聊了目录的操作,这一节我们继续来看看如何读写文件。

    首先说一下题外话,许多朋友都在摇摆不定,三心二意,其实这样的学习态度是很不好的,如果你对Windows phone开发有兴趣,如果你真想学习,你就应该一心一意,静下心来学习。

    如果你不喜欢Windows phone开发,那你不必要徘徊,你可以选择IOS、Android或者其它平台。
    只要你选择了,你应该要相信你所选择的,记得有一句话是这样说的:选择你所爱的,爱你所选择的,虽然这个比方不大合适,但意思是相近的。

    其实,说到底,不是编程有多么难学,而很多半途而废的,其根本问题就是学习态度,我们不应该说我们的长辈,像60、70后的这一辈人怎么落后,怎么跟不上时代了,对的,从知识的积累和技能上说,我们的长辈们的的确确跟不上时代了,但是,他们身上有一个优点,这个优点是我们80后,90后的年轻人身上所没有的,那就是执着,敢于吃苦的精神,这是事实,希望各位朋友要正视这一点,人不怕缺点多,就怕你不敢面对你的缺点。

    作为青春年少的我们,更应该有一种“敢于直面惨淡的人生,敢于正视淋漓的鲜血”的勇气。

    只要你喜欢,不用担心Windows phone的未来,就好像当年你不必要担心.NET的前途一个道理,也不要被一些新闻和评论吓倒,作为理性的主体,我们更应该分辨真伪,许多新闻评论都是在误导我们。
    不要管它微软动作慢不慢,市场目前是很小,但你知道,存在必有其价值,IT巨头们都在激烈竞争,作为开发者,我们只需要脚踏实地去学习。
    最近,谷歌和甲骨文的员工在努力学习法律知识,而微软的员工在努力学习市场营销学,其实从这些现象我们知道,无论开源闭源,都各有优缺点,能在二者之间取得一个平衡,才是王道。


    好了,废话就说到这里,今天的内容很简单,所以我才说那么多题外话,目的就是告诉各位WP开发者,不要浮躁,只要你能把WP开发的技能练得出神入化,哪怕它市场很小,你也能赚大钱,马宁就是一个成功案例。

    隔离存储的文件读写与我们过去在其它.NET开发中的文件读写是没有区别的,只是在WP上我们用IsolatedStorageFileStream,而不是传统的FileStream罢了,说白了,就是换了一个类名,所有操作都一样,至于你信不信,反正我是信了。

    现在,我用一个示例来证明,读写文件有多么简单。
    新建一个项目,在主页面上放一个文本框,用来输入要写入文件的内容,放两个按钮,一个用于写操作,一个用于读操作,再放一个TextBlock,用于显示从文件读入的内容。XAML布局如下所示。

    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="将内容写入到文件" Click="btnWrite_Click"/>  
    19.         </StackPanel>  
    20.         <StackPanel Margin="0,25" Orientation="Vertical">  
    21.             <Button HorizontalAlignment="Stretch" Content="从文件中读入" 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.   
    26. </phone:PhoneApplicationPage>  

    后台C#如下所示。

    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. // 引入此命名空间  
    14. using System.IO;  
    15. using System.IO.IsolatedStorage;  
    16.   
    17. namespace PhoneApp1  
    18. {  
    19.     public partial class MainPage : PhoneApplicationPage  
    20.     {  
    21.         // 常量  
    22.         const string MyDir = "MyData";  
    23.         const string testFileName = "file";  
    24.   
    25.         // 构造函数  
    26.         public MainPage()  
    27.         {  
    28.             InitializeComponent();  
    29.             this.Loaded += (sender, e) =>  
    30.                 {  
    31.                     using (var iso = IsolatedStorageFile.GetUserStoreForApplication())  
    32.                     {  
    33.                         if (iso.DirectoryExists(MyDir) == false)  
    34.                         {  
    35.                             iso.CreateDirectory(MyDir);  
    36.                         }  
    37.                     }  
    38.                 };  
    39.         }  
    40.   
    41.   
    42.         private void btnWrite_Click(object sender, RoutedEventArgs e)  
    43.         {  
    44.             using (var iso = IsolatedStorageFile.GetUserStoreForApplication())  
    45.             {  
    46.                 using (var sr = iso.CreateFile(MyDir + "\\" + testFileName))  
    47.                 {  
    48.                     StreamWriter sw = new StreamWriter(sr);  
    49.                     sw.Write(txtContent.Text);  
    50.                     sw.Close();  
    51.                     sw.Dispose();  
    52.                 }  
    53.             }  
    54.         }  
    55.   
    56.         private void btnRead_Click(object sender, RoutedEventArgs e)  
    57.         {  
    58.             using (var iso = IsolatedStorageFile.GetUserStoreForApplication())  
    59.             {  
    60.                 var sr = iso.OpenFile(MyDir + "\\" + testFileName, FileMode.Open, FileAccess.Read);  
    61.                 StreamReader reader = new StreamReader(sr);  
    62.                 string info = reader.ReadToEnd();  
    63.                 reader.Close();  
    64.                 reader.Dispose();  
    65.                 sr.Close();  
    66.                 sr.Dispose();  
    67.                 txtDisplay.Text = info;  
    68.             }  
    69.         }  
    70.     }  
    71. }  


     

    上面的代码,我想各位能看得懂的,是的,就是这么简单,现在你相信了吧。

    来,看看运后的结果吧。

  • 相关阅读:
    解决url传递过程中加号变空格的问题<转>
    windows下CEF3的关闭流程《转》
    mfc封装cef浏览器 关闭整个窗口程序得时候又重启mfc 应用的程序
    雷神免费资源
    LCA的 RMQ解法模版
    最新的js焦点图库
    whereis+whatis+man
    Anroid 手机助手 详细解析 概述(二)
    <c:forEach varStatus="status">中 varStatus的属性简介
    JBoss 系列四十九:JBoss 7/WildFly 中端口使用列表
  • 原文地址:https://www.cnblogs.com/songtzu/p/2607129.html
Copyright © 2011-2022 走看看