WP7 Isolated Storage 小结

it2022-05-09  38

先来传送门:

这个系列很不错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 程序在市场更新时,独立存储空间内以前的内容会保留。 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; }elsereturn 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 执行操作异常 经常是因为该文件所在的路径目录没有创建。

转载于:https://www.cnblogs.com/boypig/archive/2011/10/20/2218990.html

相关资源:数据结构—成绩单生成器

最新回复(0)