windows phone 文件管理

刚刚从桌面应用程序转向windows 手机开发的时候,往往对文件操作上有点纠结。 不像console application, phone app 不能够访问电脑上的文件系统,所以想访问类似"d:\\test.txt" 的文件是不行的。

windows phone 使用独立存储机制——IsolatedStorage 。 就是说,每个应用程序有其单独的存储去,不同的程序间互不可见。

IsolatedStorageSettings 比较简单,是一些键/值 对,即 <key,value> 。

1 IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
2 if (settings.Contains("Keys")){
3     string value = (string) settings["Keys"];
4 }
5 else {
6     settings.Add("Keys","value");
7 }

IsolatedStorageFile 可以对文件或目录进行操作。

1 public void SaveFile(){
2     IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();
3 
4 fileStorage.CreateDirectory("test");
5 StreamWriter wt = new StreamWriter(new IsolatedStorageFileStream("test\\test.txt",FileMode.Create,fileStorage));
6 wt.write("hello world");
7 wt.Close();
8 }

如果是读取文件的话,就改成

StreamReader rd = new StreamReader(new IsolatedSotrageFileStream("**",FileMode.Open,fileStorage));

如果想读取项目下面的某个文件,那么先把该文件的Build Action 改成 Resource,然后


Stream stream = App.GetResourceStream(

new Uri("/Name of Project;component/dir name/out.txt", UriKind.Relative)).Stream;

using (StreamReader rd = new StreamReader(stream )){。。。}

只能读不能写

 

最后提一点,Windows Phone Power Tools 这个工具很好用,可以访问windows phone 独立存储里面的文件

posted on 2012-05-18 17:29  leavingseason  阅读(2817)  评论(0编辑  收藏  举报

导航

Bye!