windows phone 文件管理

本文介绍了Windows Phone应用中的文件存储机制,包括IsolatedStorageSettings的键值对存储方式及IsolatedStorageFile对文件和目录的操作方法。同时,文中还提供了创建、读取文件的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

刚刚从桌面应用程序转向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 独立存储里面的文件

转载于:https://www.cnblogs.com/sylvanas2012/archive/2012/05/18/2508134.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值