windows phone 獨立存儲空間的操作 (2)



IsolatedStorage獨立存儲空間是保存應用程序的一些數據已經配置文件,獨立存儲空間相對於其他的wp程序是獨立的,也就是說每個wp程序都會有自己的獨立存儲空間,每個wp程序相互之間不能訪問;

什么是Isolated Storage?

Isolated Storage又叫做隔離存儲空間,Windows Phone 7手機上用來本地存儲數據。下圖是一個存儲應用的文件夾結構圖:

IC381787

Isolated Storage用來創建與維護本地存儲。WP7上的架構和Windows下的Silverlight類似,所有的讀寫操作都只限於隔離存儲空間並且無法直接訪問磁層操作系統的文件系統。這樣能夠防止非法的訪問以及其他應用的破壞,增強安全性。

提示:如果你有兩個應用想要共用一個同一個數據,則沒法通過本地存儲實現。你可以使用web服務等。

提示:WP7下的隔離存儲空間沒有配額的限制。應用應該只保存必要的數據。當Windows Phone只剩下10%的可用空間,用戶會收到一個提醒並可能停止當前應用。對用戶來講這是一個很差的用戶體驗。

在隔離存儲空間下可以進行目錄操作、文件操作、應用程序配置信息等。

 什么是Isolated Storage部分參考出處: http://www.cnblogs.com/zdave/archive/2011/05/06/2038924.html

 

 

 

  1. IsolatedStorageFile

    此類表示包含文件和目錄的獨立存儲區

    IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication();
     此類的實例化是得到整個程序的獨立存儲空間

    屬性

    AvailableFreeSpace

    表示獨立存儲的可用空間量 單位為字節

    Quota

    該值表示獨立存儲的最大可用空間量 不能超過該值 單位為字節

    方法

    CreateDirectory

    創建目錄

    CreateFile

    創建文件

    DirectoryExists

    判斷是否存在某個目錄,刪除目錄之前可調用該方法

    DeleteDirectory

    刪除創建的目錄

    FileExists

    判斷是否存在某個文件,刪除文件之前可調用該方法

    DeleteFile

    刪除傳進的文件

    GetDirectoryNames(String)

    得到匹配的目錄名稱 這里string支持通配符:單字節(“?”)和多字節(“*”)

    GetFileNames(String)

    得到匹配的文件名稱 這里string支持通配符:單字節(“?”)和多字節(“*”)

    GetUserStoreForApplication

    獲取應用程序級的獨立存儲空間

    IncreaseQuotaTo

    比較重要的方法,增加獨立存儲空間空間量,但不可超過Quota

    Remove

    移除獨立存儲區范圍及其所有內容,利用此方法必須先判斷文件和目錄是否正在使用,如果正在使用會有異常


  2. IsolatedStorageFileStream

    此類是文件流,實現對文件的操作

    IsolatedStorageFileStream isStream =  new IsolatedStorageFileStream( " test\\TEST.txt ", System.IO.FileMode.Open, FileAccess.Read, isStore);

     

    此實例化類是文件進行操作

    屬性

    CanRead

    是否可讀 默認為true

    CanSeek

    是否可檢索 默認為true

    CanWrite

    是否可寫 默認為true

    Name

    文件流寫入和讀取的路徑

    ReadTimeout

    設置的流讀取超時時間

    WriteTimeout

    設置的寫入流超時時間

    方法

    BeginRead

    在異步的時候用到,開始讀取

    EndRead

    在異步的時候用到,讀取結束

    BeginWrite

    在異步的時候用到,開始寫入

    EndWrite

    在異步的時候用到,寫入結束

    Close

    關閉當前流並釋放相關資源

    CopyTo(Stream)

    從當前流讀取所有字節並寫入目標流

    WriteByte

    寫入單個字節

    ReadByte

    讀取單個字節

    Write

    寫入字節塊bytes

    Read

    讀取文件獲得字節塊bytes

    SetLength

    限制流的長度


  3.  此類是存儲一些配置信息,實例化

     
      var settings = IsolatedStorageSettings.ApplicationSettings;

     

    // 添加內容 
    settings.Add( " key "object);
                 // 保存
                settings.Save();
     
      // 獲取配置信息
                         string value = settings[ " key "].ToString();
                         // out傳參獲取值
                         string value;
                        settings.TryGetValue( " key "out value);

     

  4. 初始化界面
    View Code
    <phone:PhoneApplicationPage 
        x:Class= " IsolatedStorageFileStreamApplication.MainPage "
        xmlns= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
        xmlns:x= " http://schemas.microsoft.com/winfx/2006/xaml "
        xmlns:phone= " clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone "
        xmlns:shell= " clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone "
        xmlns:d= " http://schemas.microsoft.com/expression/blend/2008 "
        xmlns:mc= " http://schemas.openxmlformats.org/markup-compatibility/2006 "
        mc:Ignorable= " d " d:DesignWidth= " 480 " d:DesignHeight= " 768 "
        FontFamily= " {StaticResource PhoneFontFamilyNormal} "
        FontSize= " {StaticResource PhoneFontSizeNormal} "
        Foreground= " {StaticResource PhoneForegroundBrush} "
        SupportedOrientations= " Portrait " Orientation= " Portrait "
        shell:SystemTray.IsVisible= " True ">

        <!--LayoutRoot  is the root grid  where all page content  is placed-->
        <Grid x:Name= " LayoutRoot " Background= " Transparent ">
            <Grid.RowDefinitions>
                <RowDefinition Height= " Auto "/>
                <RowDefinition Height= " * "/>
            </Grid.RowDefinitions>

            <!--TitlePanel contains the name of the application and page title-->
            <StackPanel x:Name= " TitlePanel " Grid.Row= " 0 " Margin= " 12,17,0,28 ">
                <TextBlock x:Name= " ApplicationTitle " Text= " MY APPLICATION " Style= " {StaticResource PhoneTextNormalStyle} "/>
                <TextBlock x:Name= " PageTitle " Text= " page name " Margin= " 9,-7,0,0 " Style= " {StaticResource PhoneTextTitle1Style} "/>
            </StackPanel>

            <!--ContentPanel - place additional content here-->
            <Grid x:Name= " ContentPanel " Grid.Row= " 1 " Margin= " 12,0,12,0 ">
                <Button x:Name= " btnWrite " Margin= " 0,400,300,100 " Click= " btnWrite_Click " Content= " 寫操作 "></Button>
                <Button x:Name= " btnRead " Margin= " 150,400,150,100 " Content= " 讀操作 " Click= " btnRead_Click " IsEnabled= " False " ></Button>
                <Button x:Name= " btnDel " Margin= " 300,400,10,100 " Content= " 刪除操作 " Click= " btnDel_Click "></Button>
                <TextBlock x:Name= " txtShow " Margin= " 0,100,0,200 " Text= " 顯示讀取的資料或者狀態 " HorizontalAlignment= " Center "  VerticalAlignment= " Center "></TextBlock>
            </Grid>
        </Grid>
    </phone:PhoneApplicationPage>
  5. 刪除操作
    View Code
      ///   <summary>
           
    ///  刪除文件及路徑
           
    ///   </summary>
           
    ///   <param name="sender"></param>
           
    ///   <param name="e"></param>
             private  void btnDel_Click( object sender, RoutedEventArgs e)
            {
                IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
                 // 判斷是否存在
                 if (isoStore.FileExists( " test\\TEST.txt "))
                {
                     // 刪除文件
                    isoStore.DeleteFile( " test\\TEST.txt ");
                     // 刪除目錄
                    isoStore.DeleteDirectory( " test ");
                     // 完全刪除
                    isoStore.Remove();
                     // 釋放資源
                    isoStore.Dispose();
                     // 配置信息
                     var settings = IsolatedStorageSettings.ApplicationSettings;
                     // 清空配置信息
                    settings.Clear();
                }
                txtShow.Text =  " 刪除完成 ";
              
            }

  6. 寫入操作
    View Code
    ///   <summary>
            
    ///  寫操作
            
    ///   </summary>
            
    ///   <param name="sender"></param>
            
    ///   <param name="e"></param>
             private  void btnWrite_Click( object sender, RoutedEventArgs e)
            {
                IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
                 // 獨立存儲空間可用空間量
                 long availableSpace = isoStore.AvailableFreeSpace;
                 // 獨立存儲空間最大可用空間量
                 long quota = isoStore.Quota;
                 // 獨立存儲空間的可用量擴充 字節為單位
               
    //  bool dl = isoStore.IncreaseQuotaTo(quota);
                
    // 創建目錄
                isoStore.CreateDirectory( " test ");
                IsolatedStorageFileStream isoStream =  new IsolatedStorageFileStream( " test\\TEST.txt ", System.IO.FileMode.Create, isoStore);
               
                 byte onlyOneByte =  101;
                isoStream.WriteByte(onlyOneByte);
               
                isoStream.Close();
                isoStore.Dispose();
                btnRead.IsEnabled =  true;
                txtShow.Text= " 寫入完成 ";
                 // 存儲配置信息
                 var settings = IsolatedStorageSettings.ApplicationSettings;
                 // 添加配置信息
                settings.Add( " ip "" 192.168.1.1 ");
                 // 保存
                settings.Save();

            }

  7. 讀取操作
    View Code
    ///   <summary>
            
    ///  讀操作
            
    ///   </summary>
            
    ///   <param name="sender"></param>
            
    ///   <param name="e"></param>
             private  void btnRead_Click( object sender, RoutedEventArgs e)
            {
                IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication();
           
                 if (isStore.FileExists( " test\\TEST.txt "))
                {
                    IsolatedStorageFileStream isStream =  new IsolatedStorageFileStream( " test\\TEST.txt ", System.IO.FileMode.Open, FileAccess.Read, isStore);
                     // 獲取路徑
                     string[] directoryName = isStore.GetDirectoryNames( " test ");
                     // 獲取文件名 搜索模式。 單字符("?")和多字符("*")通配符都受支持
                     string[] fileName = isStore.GetFileNames( " test\\T*.txt ");
                    txtShow.Text =  " 寫入資料值為: " + isStream.ReadByte().ToString() +  " ;\n路徑為: " + directoryName[ 0].ToString() +  " ;\n文件名稱為: " + fileName[ 0];
                    
                    isStream.Close();
                    isStore.Dispose();
                    txtShow.Text =txtShow.Text+  " \n讀取完成 ";
                     // 存儲配置信息
                     var settings = IsolatedStorageSettings.ApplicationSettings;
                   
                     // 判斷key 是否存在
                     if (settings.Contains( " ip "))
                    {
                         // 只支持key的查詢
                         string ip = settings[ " ip "].ToString();
                         // out傳參獲取值
                         string ipStr;
                        settings.TryGetValue( " ip "out ipStr);
                        txtShow.Text = txtShow.Text +  " \n配置文件ip: " + ip +  " ;\n out傳參ip: "+ipStr;
                        
                    }
                   

                }
            }

  8. 讀取效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM