在很多資料或書籍上都翻譯爲“獨立存儲”,不過,我想了一下,決定將IsolatedStorage翻譯爲“隔離存儲”,我想這樣會更方便大家對這一概念的理解。
關於何爲隔離存儲,按照固有習慣,我不希望作太多理論上的解釋,一來理論化的東西容易把簡單的事情變得複雜化,二來,就算把理論知識說得有多完美,相信大家都沒興趣看,就算你有興趣也會一頭霧水。
隔離存儲不是WP特有的,在Silverlight或WPF中也有,而且,更準確地講,“獨立存儲”在.NET 2.0的時候已經出現,可能大家沒有注意到,不信?你可以在.NET類庫中找一下。
以前沒關注過也沒關係,隔離存儲其實是很簡單的,說白了就是Windows Phone上面的目錄和文件管理,在Windows平臺上,這些操作相信做過.NET開發的朋友們都肯定玩得很熟了。
Windows phone的目錄和文件管理方式與過去Windows CE或Windows Mobile是不同的,過去在這兩個OS上都是使用相對路徑,而其操作方法與PC系統相近;而WP則不同,儘管也是使用相對路徑,但操作方式和原理不同。
這就是我爲什麼要翻譯成“隔離存儲”了,這樣一來,大家從字面上就可以猜出它的特徵了,每個應用程序只能訪問其獨立的存儲空間,你不能去訪問其它應用程序的目錄和結構,也不能訪問基礎操作系統的目錄和文件,這就大大提高了安全性。
好的,下面我們只需要一個簡單的示例,大家就會明白了。
示例允許你輸入一個目錄名稱,點擊“創建後”,將在隔離存儲中創建一個目錄,然後點擊第二個按鈕,可以檢測目錄是否存在,第三個按鈕用於刪除目錄。
- <phone:PhoneApplicationPage
- x:Class="IsoStorageSample1.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 是包含所有頁面內容的根網格-->
- <Grid x:Name="LayoutRoot" Background="Transparent">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <!--TitlePanel 包含應用程序的名稱和頁標題-->
- <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
- <TextBlock x:Name="ApplicationTitle" Text="我的應用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
- <TextBlock x:Name="PageTitle" Text="隔離存儲" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
- </StackPanel>
- <!--ContentPanel - 在此處放置其他內容-->
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <TextBox Height="72" HorizontalAlignment="Left" Margin="12,25,0,0" Name="txtDirName" VerticalAlignment="Top" Width="289" />
- <Button Content="創建" Height="72" HorizontalAlignment="Left" Margin="307,25,0,0" Name="btnCreate" VerticalAlignment="Top" Width="127" Click="btnCreate_Click" />
- <Button Content="檢測目錄是否已存在" Height="72" HorizontalAlignment="Left" Margin="146,120,0,0" Name="btnCheck" VerticalAlignment="Top" Width="276" Click="btnCheck_Click" />
- <Button Content="刪除目錄" Height="72" HorizontalAlignment="Left" Margin="0,283,0,0" Name="btnDel" VerticalAlignment="Top" Width="422" Click="btnDel_Click" />
- <TextBlock Height="38" HorizontalAlignment="Left" Margin="9,138,0,0" Name="tbDisplay" VerticalAlignment="Top" Width="131" />
- </Grid>
- </Grid>
- </phone:PhoneApplicationPage>
<phone:PhoneApplicationPage
x:Class="IsoStorageSample1.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 是包含所有頁面內容的根網格-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel 包含應用程序的名稱和頁標題-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="我的應用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="隔離存儲" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - 在此處放置其他內容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBox Height="72" HorizontalAlignment="Left" Margin="12,25,0,0" Name="txtDirName" VerticalAlignment="Top" Width="289" />
<Button Content="創建" Height="72" HorizontalAlignment="Left" Margin="307,25,0,0" Name="btnCreate" VerticalAlignment="Top" Width="127" Click="btnCreate_Click" />
<Button Content="檢測目錄是否已存在" Height="72" HorizontalAlignment="Left" Margin="146,120,0,0" Name="btnCheck" VerticalAlignment="Top" Width="276" Click="btnCheck_Click" />
<Button Content="刪除目錄" Height="72" HorizontalAlignment="Left" Margin="0,283,0,0" Name="btnDel" VerticalAlignment="Top" Width="422" Click="btnDel_Click" />
<TextBlock Height="38" HorizontalAlignment="Left" Margin="9,138,0,0" Name="tbDisplay" VerticalAlignment="Top" Width="131" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
- private void btnCreate_Click(object sender, RoutedEventArgs e)
- {
- // 通過GetUserStoreForApplication靜態方法,可以返回一個IsolatedStorageFile實例。
- IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
- try
- {
- if (iso.DirectoryExists(txtDirName.Text))
- {
- return;
- }
- iso.CreateDirectory(this.txtDirName.Text);
- }
- catch
- { MessageBox.Show("Error !"); }
- }
- private void btnCheck_Click(object sender, RoutedEventArgs e)
- {
- IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
- bool exists = iso.DirectoryExists(txtDirName.Text);
- if (exists)
- {
- this.tbDisplay.Text="已存在";
- }
- else
- {
- this.tbDisplay.Text = "不存在";
- }
- }
- private void btnDel_Click(object sender, RoutedEventArgs e)
- {
- IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
- try
- {
- iso.DeleteDirectory(txtDirName.Text);
- }
- catch
- {
- MessageBox.Show("Error !");
- }
- }
private void btnCreate_Click(object sender, RoutedEventArgs e)
{
// 通過GetUserStoreForApplication靜態方法,可以返回一個IsolatedStorageFile實例。
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
try
{
if (iso.DirectoryExists(txtDirName.Text))
{
return;
}
iso.CreateDirectory(this.txtDirName.Text);
}
catch
{ MessageBox.Show("Error !"); }
}
private void btnCheck_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
bool exists = iso.DirectoryExists(txtDirName.Text);
if (exists)
{
this.tbDisplay.Text="已存在";
}
else
{
this.tbDisplay.Text = "不存在";
}
}
private void btnDel_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
try
{
iso.DeleteDirectory(txtDirName.Text);
}
catch
{
MessageBox.Show("Error !");
}
}
注意,要引入System.IO.IsolatedStorage命名空間。