IsolatedStorage獨立存儲空間是儲存應用程式的一些資料已經配置檔案,獨立存儲空間相對于其他的wp程式是獨立的,也就是說每個wp程式都會有自己的獨立存儲空間,每個wp程式互相之間不能通路;
什麼是Isolated Storage?
Isolated Storage又叫做隔離存儲空間,Windows Phone 7手機上用來本地存儲資料。下圖是一個存儲應用的檔案夾結構圖:

Isolated Storage用來建立與維護本地存儲。WP7上的架構和Windows下的Silverlight類似,所有的讀寫操作都隻限于隔離存儲空間并且無法直接通路磁層作業系統的檔案系統。這樣能夠防止非法的通路以及其他應用的破壞,增強安全性。
提示:如果你有兩個應用想要共用一個同一個資料,則沒法通過本地存儲實作。你可以使用web服務等。
提示:WP7下的隔離存儲空間沒有配額的限制。應用應該隻儲存必要的資料。當Windows Phone隻剩下10%的可用空間,使用者會收到一個提醒并可能停止目前應用。對使用者來講這是一個很差的使用者體驗。
在隔離存儲空間下可以進行目錄操作、檔案操作、應用程式配置資訊等。
什麼是Isolated Storage部分參考出處: http://www.cnblogs.com/zdave/archive/2011/05/06/2038924.html
-
IsolatedStorageFile類
此類表示包含檔案和目錄的獨立存儲區
IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication();
此類的執行個體化是得到整個程式的獨立存儲空間
屬性
方法AvailableFreeSpace 表示獨立存儲的可用空間量 機關為位元組 Quota 該值表示獨立存儲的最大可用空間量 不能超過該值 機關為位元組 CreateDirectory 建立目錄 CreateFile 建立檔案 DirectoryExists 判斷是否存在某個目錄,删除目錄之前可調用該方法 DeleteDirectory 删除建立的目錄 FileExists 判斷是否存在某個檔案,删除檔案之前可調用該方法 DeleteFile 删除傳進的檔案 GetDirectoryNames(String) 得到比對的目錄名稱 這裡string支援通配符:單位元組(“?”)和多位元組(“*”) GetFileNames(String) 得到比對的檔案名稱 這裡string支援通配符:單位元組(“?”)和多位元組(“*”) GetUserStoreForApplication 擷取應用程式級的獨立存儲空間 IncreaseQuotaTo 比較重要的方法,增加獨立存儲空間空間量,但不可超過Quota Remove 移除獨立存儲區範圍及其所有内容,利用此方法必須先判斷檔案和目錄是否正在使用,如果正在使用會有異常 -
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 限制流的長度 -
IsolatedStorageSettings類
var settings = IsolatedStorageSettings.ApplicationSettings;
//添加内容
settings.Add("key", object);
//儲存
settings.Save();
//擷取配置資訊
string value = settings["key"].ToString();
//out傳參擷取值
string value;
settings.TryGetValue("key", out value);
- 初始化界面
window phone 獨立存儲空間的操作 window phone 獨立存儲空間的操作 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>
</phone:PhoneApplicationPage>
window phone 獨立存儲空間的操作 - 删除操作
window phone 獨立存儲空間的操作 window phone 獨立存儲空間的操作 /// <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 = "删除完成";
- 寫入操作
window phone 獨立存儲空間的操作 window phone 獨立存儲空間的操作 /// 寫操作
private void btnWrite_Click(object sender, RoutedEventArgs e)
//獨立存儲空間可用空間量
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();
btnRead.IsEnabled = true;
txtShow.Text="寫入完成";
//存儲配置資訊
//添加配置資訊
settings.Add("ip", "192.168.1.1");
- 讀取操作
window phone 獨立存儲空間的操作 window phone 獨立存儲空間的操作 window phone 獨立存儲空間的操作 /// 讀操作
private void btnRead_Click(object sender, RoutedEventArgs e)
if (isStore.FileExists("test\\TEST.txt"))
//擷取路徑
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讀取完成";
//判斷key 是否存在
if (settings.Contains("ip"))
//隻支援key的查詢
string ip = settings["ip"].ToString();
string ipStr;
settings.TryGetValue("ip", out ipStr);
txtShow.Text = txtShow.Text + "\n配置檔案ip:" + ip + ";\n out傳參ip:"+ipStr;
- 讀取效果
window phone 獨立存儲空間的操作