首先,我先贴出测试工程的代码。随便建一个windows phone 的工程
在MainPage.xaml中


<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBox Height="172" HorizontalAlignment="Left" Margin="74,60,0,0" Name="SText" Text="" VerticalAlignment="Top" Width="298" />
<Button Content="Load" Height="72" HorizontalAlignment="Left" Margin="24,315,0,0" Name="BTLoad" VerticalAlignment="Top" Width="160" Click="BTLoad_Click" />
<Button Content="Save" Height="72" HorizontalAlignment="Left" Margin="242,315,0,0" Name="BTSave" VerticalAlignment="Top" Width="160" Click="BTSave_Click" />
</Grid>
</Grid>
在MainPage.xaml.cs中
private void BTSave_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();
////第一种方法
appStorage.CreateDirectory("x");
appStorage.CreateDirectory("NewFolder");
using (var file = appStorage.OpenFile("NewFolder\\Note.txt", System.IO.FileMode.OpenOrCreate))
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(file))
{
sw.WriteLine(SText.Text);
}
}
//第二种方法
//appStorage.CreateDirectory("x");
//StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream("x/aaa.txt", FileMode.Create, appStorage));
//writer.Write(SText.Text);
//writer.Close();
}
private void BTLoad_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication();
////第一种方法
using (var file = appStorage.OpenFile("NewFolder\\Note.txt", System.IO.FileMode.Open))
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(file))
{
SText.Text = sr.ReadToEnd();
}
}
//第二种方法
//StreamReader reader = null;
//try
//{
// reader = new StreamReader(new IsolatedStorageFileStream("x/aaa.txt", FileMode.Open, appStorage));
// SText.Text = reader.ReadToEnd();
// reader.Close();
//}
//catch
//{
// SText.Text = "Need to create directory and the file first.";
//}
}
}
}
其次,我们来了解一下我们所用的工具ISETool。工具是随着SDK安装的。
工具所在位置:
· Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorerTool
· Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorerTool
该工具的命令行语法:
ISETool.exe < ts|rs|dir[:device-folder]> <xd|de> <Product GUID> [<desktop-path>]
下表列出了命令行参数的含义.
操作 | 描述 |
---|---|
ts | (Take snapshot) 拷贝模拟器或真机的独立存储区文件或目录到计算机. |
rs | (Restore snapshot) 替换模拟器或真机的独立存储区的文件或目录从计算机. |
dir | 列出指定独立存储区目录下的文件或目录. 如果未指定路径, 将列出根目录的文件及目录. |
device-folder | 指定在模拟器或真机中的独立存储区的你要定位的目标路径 |
xd | 表示目标是模拟器. |
de | 表示目标是真机. |
Product GUID | 指定你想要的程序的WPAppManifest.xml文件中的ProductID |
desktop-path | 指定向独立存储区替换或从其复制文件到计算机的的路径. 使用ts命令后将会拷贝一个命名为IsolatedStore的子目录在desktop-path中. 如果指定的目录已经存在,ts命令将会没有任何警告覆盖其目录下的内容 . |
开始测试ISETool:
(1)找到工程xap文件。将工程编译一遍,保证程序不崩溃可执行。找到工程bin目录的*.xap文件。找到这个文件用于向模拟器部署该程序。比如我的文件在C:\Users\xxx\Documents\Visual Studio 2010\Projects\PhoneApp1\PhoneApp1\Bin\Debug下
(2)部署应用程序。打开 开始->所有程序->Windows Phone Developer Tools->Application Deployment。
打开程序部署工具后。选择真机或者模拟器,因为木有真机,我就选模拟器,然后找到xap的位置。
点Deploy就将程序部署到模拟器中了。
(3)运行cmd。通过执行工具ISETool,可以查看,复制,替换独立存储区的文件或文件夹。
C:\Users\xxxxxxx>cd C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\Is
olatedStorageExplorerTool
C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorer
Tool>
(4)找到程序的ProductID。打开程序的WMAppManifest.xml文件。
“77a2c580-4bf0-48f7-a1d0-3f76166d337b”这个就是传说中的ProductID。
(4)运行工具。具体操作范例见下面。
[查看]
未执行存储操作查看独立存储空间
C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorer
Tool>ISETool.exe dir xd 77a2c580-4bf0-48f7-a1d0-3f76166d337b
<DIR> Shared
向独立存储区存了文件
C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorer
Tool>ISETool.exe dir xd 77a2c580-4bf0-48f7-a1d0-3f76166d337b
<DIR> Shared
<DIR> x
<DIR> NewFolder
查看文件夹下文件
C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorer
Tool>ISETool.exe dir:\NewFolder\ xd 77a2c580-4bf0-48f7-a1d0-3f76166d337b
8 Note.txt
[复制] 将独立存储区得文件复制到电脑可见位置
C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorer
Tool>ISETool.exe ts xd 77a2c580-4bf0-48f7-a1d0-3f76166d337b "e:\Temp"
Download Started ... Into Folder: e:\Temp
Download Successful Into Folder: e:\Temp
复制成功,结果自己去E:\Temp看看吧
[替换]
修改E:\Temp\IsolatedStore\Note.txt里的内容,然后
C:\Program Files\Microsoft SDKs\Windows Phone\v7.1\Tools\IsolatedStorageExplorer
Tool>ISETool.exe rs xd 77a2c580-4bf0-48f7-a1d0-3f76166d337b "e:\Temp\IsolatedStore"
Upload Started ... From Folder: e:\Temp\IsolatedStore
Upload Successful From Folder: e:\Temp\IsolatedStore
从程序读出独立存储区的内容,是不是发现内容已经变化了。
具体内容可参见msdn。http://msdn.microsoft.com/en-us/library/hh286408(v=VS.92).aspx
这个工具有啥用?比如每次启动模拟器,程序相当于卸载再重新部署,存储的文件内容就找不到了。我们可以通过该工具备份我们需要的数据。比如我们可以很方便的修改独立存储区的内容,然后对各种数据进行测试。这个对于我们测试代码来说十分有用。使用ISETool吧朋友们!