<p>1、設定與屬性的存儲</p><p>2、本地檔案的存儲</p>
</pre><pre name="code" class="csharp">//獨立存儲 鍵值對IsolatedStorageSettings(獨立本地設定): 命名空間為:System.IO.IsolatedStorage.IsolatedStorageSettings,提供了一系列的API,用來在獨立存儲中存儲和操作鍵/值對。一般使用該方式來存儲App設定和使用者特定設定。
IsolatedStorageSettings mysetting = IsolatedStorageSettings.ApplicationSettings;
protected override void OnNavigatedFrom(NavigationEventArgs e)//重寫onnavigatefrom
{
base.OnNavigatedFrom(e);
mysetting["text"] = this.tex.Text;//textbox設定存儲
mysetting["check"] = checkbox.IsChecked;//checkbox的設定存儲
mysetting["radio1"] = radio1.IsChecked;//radio設定存儲
mysetting["radio2"] = radio2.IsChecked;
mysetting.Save();
}
protected override void OnNavigatedTo(NavigationEventArgs e)//重寫onnaviatefrom
{
base.OnNavigatedTo(e);
if(mysetting.Contains("text"))
tex.Text = mysetting["text"] as string;
if (mysetting.Contains("check"))
{
checkbox.IsChecked = (bool)mysetting["check"];
}
if(mysetting.Contains("radio1"))
{
radio1.IsChecked = (bool)mysetting["radio1"];
}
if (mysetting.Contains("radio2"))
{
radio2.IsChecked = (bool)mysetting["radio2"];
}
}
//命名空間為:System.IO.IsolatedStorage.IsolatedStorageFile,可以在虛拟的獨立存儲中建立、使用、删除檔案和目錄, 使用System.IO.IsolatedStorage.IsolatedFileStream,通過檔案流(file stream)可以添加或者檢索檔案。獨立檔案流可以存儲從web動态加載的圖檔、聲音和檔案。
IsolatedStorageFile filestorage = IsolatedStorageFile.GetUserStoreForApplication();//執行個體化IsolatedStorageFile對象
//往isolatedstorage寫資料
private void xie_Click(object sender, RoutedEventArgs e)
{
//建立檔案夾
// 首先判斷目錄是否存在,不存在的話建立目錄
if (!filestorage.DirectoryExists("myFolder"))
{
filestorage.CreateDirectory("myFolder");
}
//建立檔案并寫入資料
using (var isofilestream = new IsolatedStorageFileStream("myFolder/storage.txt", FileMode.OpenOrCreate, filestorage))
{
using (var filewriter = new StreamWriter(isofilestream))
{
filewriter.WriteLine(this.tex.Text);
}
}
}
從Isolated Storage中讀取資料
private void du_Click(object sender, RoutedEventArgs e)
{
try
{
using (var isofilestream = new IsolatedStorageFileStream("myFolder/storage.txt", FileMode.Open, filestorage))
{
using (var filereader = new StreamReader(isofilestream))
{
this.textbolck.Text = filereader.ReadLine();//檔案
}
}
}
catch {
this.textbolck.Text = "讀取出錯,請先建立檔案夾與檔案";
}
}
}