天天看點

window phone 獨立存儲空間的操作IsolatedStorageFile類IsolatedStorageFileStream類

IsolatedStorage獨立存儲空間是儲存應用程式的一些資料已經配置檔案,獨立存儲空間相對于其他的wp程式是獨立的,也就是說每個wp程式都會有自己的獨立存儲空間,每個wp程式互相之間不能通路;

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

<a href="http://images.cnblogs.com/cnblogs_com/zdave/201105/201105061434096484.gif"></a>

Isolated Storage用來建立與維護本地存儲。WP7上的架構和Windows下的Silverlight類似,所有的讀寫操作都隻限于隔離存儲空間并且無法直接通路磁層作業系統的檔案系統。這樣能夠防止非法的通路以及其他應用的破壞,增強安全性。

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

提示:WP7下的隔離存儲空間沒有配額的限制。應用應該隻儲存必要的資料。當Windows Phone隻剩下10%的可用空間,使用者會收到一個提醒并可能停止目前應用。對使用者來講這是一個很差的使用者體驗。

在隔離存儲空間下可以進行目錄操作、檔案操作、應用程式配置資訊等。

此類表示包含檔案和目錄的獨立存儲區

IsolatedStorageFile isStore = IsolatedStorageFile.GetUserStoreForApplication();

此類的執行個體化是得到整個程式的獨立存儲空間

屬性

<a href="http://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage.isolatedstoragefile.availablefreespace%28v=vs.95%29.aspx">AvailableFreeSpace</a>

表示獨立存儲的可用空間量 機關為位元組

<a href="http://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage.isolatedstoragefile.quota%28v=vs.95%29.aspx">Quota</a>

該值表示獨立存儲的最大可用空間量 不能超過該值 機關為位元組

方法

<a href="http://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage.isolatedstoragefile.createdirectory%28v=vs.95%29.aspx">CreateDirectory</a>

建立目錄

<a href="http://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage.isolatedstoragefile.createfile%28v=vs.95%29.aspx">CreateFile</a>

建立檔案

<a href="http://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage.isolatedstoragefile.directoryexists%28v=vs.95%29.aspx">DirectoryExists</a>

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

<a href="http://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage.isolatedstoragefile.deletedirectory%28v=vs.95%29.aspx">DeleteDirectory</a>

删除建立的目錄

<a href="http://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage.isolatedstoragefile.fileexists%28v=vs.95%29.aspx">FileExists</a>

判斷是否存在某個檔案,删除檔案之前可調用該方法

<a href="http://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage.isolatedstoragefile.deletefile%28v=vs.95%29.aspx">DeleteFile</a>

删除傳進的檔案

<a href="http://msdn.microsoft.com/zh-cn/library/20cha7ch%28v=vs.95%29.aspx">GetDirectoryNames(String)</a>

得到比對的目錄名稱 這裡string支援通配符:單位元組(“?”)和多位元組(“*”)

<a href="http://msdn.microsoft.com/zh-cn/library/c02ys433%28v=vs.95%29.aspx">GetFileNames(String)</a>

得到比對的檔案名稱 這裡string支援通配符:單位元組(“?”)和多位元組(“*”)

<a href="http://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage.isolatedstoragefile.getuserstoreforapplication%28v=vs.95%29.aspx">GetUserStoreForApplication</a>

擷取應用程式級的獨立存儲空間

<a href="http://msdn.microsoft.com/zh-cn/library/system.io.isolatedstorage.isolatedstoragefile.increasequotato%28v=vs.95%29.aspx">IncreaseQuotaTo</a>

<a href="http://msdn.microsoft.com/zh-cn/library/7ay840d1%28v=vs.95%29.aspx">Remove</a>

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

此類是檔案流,實作對檔案的操作

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

此執行個體化類是檔案進行操作

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.canread%28v=vs.95%29.aspx">CanRead</a>

是否可讀 預設為true

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.canseek%28v=vs.95%29.aspx">CanSeek</a>

是否可檢索 預設為true

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.canwrite%28v=vs.95%29.aspx">CanWrite</a>

是否可寫 預設為true

<a href="http://msdn.microsoft.com/en-us/library/system.io.filestream.name%28v=vs.95%29.aspx">Name</a>

檔案流寫入和讀取的路徑

<a href="http://msdn.microsoft.com/en-us/library/system.io.stream.readtimeout%28v=vs.95%29.aspx">ReadTimeout</a>

設定的流讀取逾時時間

<a href="http://msdn.microsoft.com/en-us/library/system.io.stream.writetimeout%28v=vs.95%29.aspx">WriteTimeout</a>

設定的寫入流逾時時間

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.beginread%28v=vs.95%29.aspx">BeginRead</a>

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

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.endread%28v=vs.95%29.aspx">EndRead</a>

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

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.beginwrite%28v=vs.95%29.aspx">BeginWrite</a>

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

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.endwrite%28v=vs.95%29.aspx">EndWrite</a>

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

<a href="http://msdn.microsoft.com/en-us/library/system.io.stream.close%28v=vs.95%29.aspx">Close</a>

關閉目前流并釋放相關資源

<a href="http://msdn.microsoft.com/en-us/library/dd782932%28v=vs.95%29.aspx">CopyTo(Stream)</a>

從目前流讀取所有位元組并寫入目标流

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.writebyte%28v=vs.95%29.aspx">WriteByte</a>

寫入單個位元組

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.readbyte%28v=vs.95%29.aspx">ReadByte</a>

讀取單個位元組

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.write%28v=vs.95%29.aspx">Write</a>

寫入位元組塊bytes

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.read%28v=vs.95%29.aspx">Read</a>

讀取檔案獲得位元組塊bytes

<a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefilestream.setlength%28v=vs.95%29.aspx">SetLength</a>

限制流的長度

此類是存儲一些配置資訊,執行個體化

var settings = IsolatedStorageSettings.ApplicationSettings;

//添加内容 

settings.Add("key", object);

//儲存

settings.Save();

//擷取配置資訊

string value = settings["key"].ToString();

//out傳參擷取值

string value;

settings.TryGetValue("key", out value);

初始化界面

View Code

&lt;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"&gt;

&lt;!--LayoutRoot is the root grid where all page content is placed--&gt;

&lt;Grid x:Name="LayoutRoot" Background="Transparent"&gt;

&lt;Grid.RowDefinitions&gt;

&lt;RowDefinition Height="Auto"/&gt;

&lt;RowDefinition Height="*"/&gt;

&lt;/Grid.RowDefinitions&gt;

&lt;!--TitlePanel contains the name of the application and page title--&gt;

&lt;StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"&gt;

&lt;TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/&gt;

&lt;TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/&gt;

&lt;/StackPanel&gt;

&lt;!--ContentPanel - place additional content here--&gt;

&lt;Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"&gt;

&lt;Button x:Name="btnWrite" Margin="0,400,300,100" Click="btnWrite_Click" Content="寫操作"&gt;&lt;/Button&gt;

&lt;Button x:Name="btnRead" Margin="150,400,150,100" Content="讀操作" Click="btnRead_Click"IsEnabled="False" &gt;&lt;/Button&gt;

&lt;Button x:Name="btnDel" Margin="300,400,10,100" Content="删除操作" Click="btnDel_Click"&gt;&lt;/Button&gt;

&lt;TextBlock x:Name="txtShow" Margin="0,100,0,200" Text="顯示讀取的資料或者狀态" HorizontalAlignment="Center" VerticalAlignment="Center"&gt;&lt;/TextBlock&gt;

&lt;/Grid&gt;

&lt;/phone:PhoneApplicationPage&gt;

删除操作

/// &lt;summary&gt;

/// 删除檔案及路徑

/// &lt;/summary&gt;

/// &lt;param name="sender"&gt;&lt;/param&gt;

/// &lt;param name="e"&gt;&lt;/param&gt;

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();

//配置資訊

//清空配置資訊

settings.Clear();

}

txtShow.Text = "删除完成";

寫入操作

/// 寫操作

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");

讀取操作

/// 讀操作

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;

讀取效果

<b></b>

<b>本文轉自shenzhoulong  51CTO部落格,原文連結:http://blog.51cto.com/shenzhoulong/816473,如需轉載請自行聯系原作者</b>