using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
namespace Day15_IsolatedStorage
{
public partial class MainPage : PhoneApplicationPage
{
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
using System.IO.IsolatedStorage;
using System.IO;
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
//Obtain a virtual store for application
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Create new subdirectory
fileStorage.CreateDirectory("textFiles");
//Create a new StreamWriter, to write the file to the specified location.
StreamWriter fileWriter = new StreamWriter(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.OpenOrCreate, fileStorage));
//Write the contents of our TextBox to the file.
fileWriter.WriteLine(writeText.Text);
//Close the StreamWriter.
fileWriter.Close();
}
private void GetButton_Click(object sender, RoutedEventArgs e)
{
//Obtain a virtual store for application
IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
//Create a new StreamReader
StreamReader fileReader = null;
try
{
//Read the file from the specified location.
fileReader = new StreamReader(new IsolatedStorageFileStream("textFiles\\newText.txt", FileMode.Open, fileStorage));
//Read the contents of the file (the only line we created).
string textFile = fileReader.ReadLine();
//Write the contents of the file to the TextBlock on the page.
viewText.Text = textFile;
fileReader.Close();
}
catch
{
//If they click the view button first, we need to handle the fact that the file hasn't been created yet.
viewText.Text = "Need to create directory and the file first.";
}
}离开程序时这多像一个迷人的魔术,再回来时,会再次载入文件(它还在那儿!)。 你都知道了。现在我们在Windows Phone 7中有两种存储机制可以用。IsolatedStorageSettings和IsolatedStorageFile。我很乐意听到你在程序中使用这两种存储结构的创新用法。请留言! 下载代码示例
这个例子将上面展示的代码融合到了一个项目中。
原文地址:http://www.jeffblankenburg.com/post/31-Days-of-Windows-Phone-7c-Day-15-Isolated-Storage.aspx