WP7在设计的时候确实比较封闭,应用程序没有直接操作闪存的权限,读取文件只能在系统专为该应用设定的目录内。虽然现在WP7上也有文件管理器,但都不是很完美,比如我的i917,在升了7712以后,网上所有的文件管理器都没用。 布局比较简单,上个图吧,代码就不贴了,下面再加上打包的源代码。 解释一下,TextBox用于输入保存到文件的内容,TextBlock用于显示读取到的文件内容。WP7的IsolatedStorage没有文件大小配额限制 C# 代码:
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.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Collections;
using System.IO.IsolatedStorage;
namespace PhoneApp4
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void button1_Click_1(object sender, RoutedEventArgs e)
{
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
store.CreateDirectory("image");
//创建image目录
IsolatedStorageFileStream storeStream = new IsolatedStorageFileStream("image\\a.txt", FileMode.OpenOrCreate, store);
//打开或创建(不存在)文件,得到流
//也可以使用 IsolatedStorageFileStream storeStream = store.OpenFile("image\\a.txt", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(storeStream);
sw.WriteLine(textBox1.Text);
//写入
sw.Close();
//关闭流
}
private void button2_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream storeStream = new IsolatedStorageFileStream("image\\a.txt", FileMode.OpenOrCreate, store);
StreamReader sd = new StreamReader(storeStream);
//读取流
textBlock1.Text=sd.ReadLine();
//前面写的时候只写了一行,所以不用循环了
//多行可以用ReadToEnd()方法
sd.Close();
//关闭流
}
}
}
源码打包: [download id=”25”]