WP7 Isolated Storage 删除文件
来源:互联网 发布:ie修复软件绿色版 编辑:程序博客网 时间:2024/05/21 10:09
先来传送门:
这个系列很不错http://www.windowsphonegeek.com/tips/all-about-wp7-isolated-storage--intro-to-isolated-storage
当然我们国内也有翻译版 http://www.cnblogs.com/zdave/archive/2011/06/10/2077443.html
有几个注意点
- 1 程序卸载时,会自动清除对应的独立存储空间。
- 2 程序在市场更新时,独立存储空间内以前的内容会保留。
- 3 IsolatedStorageSettings 不是线程安全的,如果同时使用多线程时会报 IsolatedStorageException 异常。
- 4 独立存储空间意味着是独立的,如果两个程序想要共享一块内容,可以使用web serverce 可以使得任意多的程序共享资源。
- 5 一个目录被删除时必须为空, 否则会报异常。
这里我通过递归的方法解决这个问题。
public static void DeleteDirectory(string directoryPath) { if (DirectoryExists(directoryPath)) { string[] fileNames = myIsolatedStorage.GetFileNames(directoryPath+"\\*"); string[] dirctoryNames = myIsolatedStorage.GetDirectoryNames(directoryPath+"\\*"); if (fileNames.Length> 0) { foreach (var item in fileNames) { myIsolatedStorage.DeleteFile(System.IO.Path.Combine(directoryPath, item)); } } if (dirctoryNames.Length > 0) { foreach (var item in dirctoryNames) { DeleteDirectory(System.IO.Path.Combine(directoryPath, item)); } } myIsolatedStorage.DeleteDirectory(directoryPath); } }
- 6 创建文件时 首先检查文件所在的目录是否存在 如果不存在会引发Operation not permitted on IsolatedStorageFileStream 异常。
创建文件完成后需要及时释放流。 否则再次使用时仍然会导致异常。
public static bool CreatFile(string filePath) { try { if (!string.IsNullOrEmpty(filePath) && !FileExists(filePath)) { string directoryName = System.IO.Path.GetDirectoryName(filePath); if (!string.IsNullOrEmpty(directoryName) && !DirectoryExists(directoryName)) { CreatDirectory(directoryName); } myIsolatedStorage.CreateFile(filePath).Close(); return true; } else return false; } catch (Exception ex) { return false; } }
- 7 注意使用 try{}catch{}来捕获异常
- 8 使用 using(){ } 可以使 IDisposable 接口的继承对象及时释放资源(即调用了dispose())
- 9 不要以明文形式在 IsolatedStorageSettings 中存储密码,尝试进行加密。
另外几个个人常用知识点:
1 System.IO.Path.Combine(directoryPath, item) 用来合并路径。
2 System.IO.Path.GetDirectoryName(filePath) 用来分理处路径中的 目录
3 GetFileNames()方法 里面需要在路径后添加上“*”(所有文件名) “?”(匹配的文件名) 否则读取不出文件全集。
同理 GetDirectoryName() 方法也需要如此处理。
4 不允许对 IsolatedStorageFileStream 执行操作异常 经常是因为该文件所在的路径目录没有创建。
- WP7 Isolated Storage 删除文件
- WP7 Isolated Storage详解 读取、保存图片文件
- WP7 Isolated Storage 系列 - 1.Isolated Storage 简介
- WP7 Isolated Storage 系列 - 5.使用XmlSerializer读取和存储XML文件
- WP7 Isolated Storage 系列 - 6.使用XmlWriter读取和存储XML文件
- WP7-Windows Phone的Isolated Storage Explorer使用指南
- WP7 Isolated Storage 系列 - 2.创建文件夹和文件
- WP7 Isolated Storage 系列 - 3.使用IsolatedStorageSettings存储数据
- WP7 Isolated Storage 系列 - 4.读取和存储文本文件
- WP7 Isolated Storage 系列 - 7.读取和存储图像
- Isolated Storage
- Extracting a SQL CE DB from Isolated Storage in WP7 Mango
- 隔离存贮(Isolated Storage)
- Isolated Storage 独立存储
- Silverlight Isolated Storage 独立存储
- Silverlight Isolated Storage 独立存储
- Silverlight 的 Isolated Storage 学习笔记
- Silverlight 2 Beta 2的Isolated Storage
- Android 2.3.3 近场通信NFC介绍
- 软件的80%定律
- 2012-03-28-2
- 分享60个超棒的Vector图标: Jigsoar Icons
- Java EE 6无法安装的解决方法
- WP7 Isolated Storage 删除文件
- 第六周任务二(填空题)
- Jquery插件--实现面板模拟效果
- 【java与模式】适配器模式
- snmp自动发trap的实现
- 转帖:为WinCE平台编译SQLite3.7动态库
- 黑马日记第六篇——java常用API
- A C# Reading List by Eric Lippert (ZZ)
- awk