05,WP8的文件和存储

内容预告:

  • 特殊的文件夹(Shared/Media,Shared/ShellContent,Shared/Transfer)
  • 用ISET浏览本地文件夹
  • 后台文件传输
  • 使用SD存储卡

但不包括:

  • 本地数据库(基于LINQ的sqlce)
  • SQLite

本地数据存储概览:打包管理器把所有的App放到"安装文件夹",App存储数据到"本地文件夹"。

定位存储位置的不同方式:

WP8文件存储的备选方案:三种方式

复制代码
// WP7.1 IsolatedStorage APIs
var isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fs = new IsolatedStorageFileStream("CaptainsLog.store", FileMode.Open, isf));...// WP8 Storage APIs using URI
StorageFile storageFile = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(                        
new Uri("ms-appdata:///local/CaptainsLog.store "));
...// WP8 Storage APIs
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile storageFile = await localFolder.GetFileAsync("CaptainsLog.store");
复制代码

用WP7.1的方式:

IsolatedStorage类在System.IO.IsolatedStorage命名空间里:

  • IsolatedStorage,在独立存储区表达文件和文件夹
  • IsolatedFileStream,在IsolatedStorage中暴露一个文件流到文件存储。
  • IsolatedStorageSettings,以键值对(Dictionary<TKey,TValue>)方式存储。

保存数据:

复制代码
private void saveGameToIsolatedStorage(string message){  using (IsolatedStorageFile isf =         IsolatedStorageFile.GetUserStoreForApplication())  {    using (IsolatedStorageFileStream rawStream = isf.CreateFile("MyFile.store"))    {       StreamWriter writer = new StreamWriter(rawStream);       writer.WriteLine(message); // save the message       writer.Close();    }  }}
复制代码

读取数据:

复制代码
private void saveGameToIsolatedStorage(string message){  using (IsolatedStorageFile isf =         IsolatedStorageFile.GetUserStoreForApplication())  {    using (IsolatedStorageFileStream rawStream = isf.CreateFile("MyFile.store"))    {       StreamWriter writer = new StreamWriter(rawStream);       writer.WriteLine(message); // save the message       writer.Close();    }  }}
复制代码

保存键值对数据:记着在最后调用Save函数保存。

复制代码
void saveString(string message, string name){   IsolatedStorageSettings.ApplicationSettings[name] =     message;    IsolatedStorageSettings.ApplicationSettings.Save();}
复制代码

读取键值对数据:记着要先检查是否有这个键,否则要抛异常。

复制代码
string loadString(string name){  if (IsolatedStorageSettings.ApplicationSettings.Contains(name))  {      return (string)           IsolatedStorageSettings.ApplicationSettings[name];    }    else        return null;}
复制代码

WinPRT的存储方式:在Windows.Storage命名空间下:

  • StorageFolder
  • StorageFile
  • 不支持ApplicationData.LocalSettings,只能用IsolatedStorageSettings或自定义文件

用StorageFolder保存数据:

复制代码
private async void saveGameToIsolatedStorage(string message){     // Get a reference to the Local Folder     Windows.Storage.StorageFolder localFolder =          Windows.Storage.ApplicationData.Current.LocalFolder;    // Create the file in the local folder, or if it already exists, just open it    Windows.Storage.StorageFile storageFile =                 await localFolder.CreateFileAsync("Myfile.store",                           CreationCollisionOption.OpenIfExists);    Stream writeStream = await storageFile.OpenStreamForWriteAsync();    using (StreamWriter writer = new StreamWriter(writeStream))    {        await writer.WriteAsync(logData);    }}
复制代码

读取数据:

复制代码
private async void saveGameToIsolatedStorage(string message){     // Get a reference to the Local Folder     Windows.Storage.StorageFolder localFolder =          Windows.Storage.ApplicationData.Current.LocalFolder;    // Create the file in the local folder, or if it already exists, just open it    Windows.Storage.StorageFile storageFile =                 await localFolder.CreateFileAsync("Myfile.store",                           CreationCollisionOption.OpenIfExists);    Stream writeStream = await storageFile.OpenStreamForWriteAsync();    using (StreamWriter writer = new StreamWriter(writeStream))    {        await writer.WriteAsync(logData);    }}
复制代码

ms-appdata:/// or ms-appx:/// 保存文件:

复制代码
// There's no FileExists method in WinRT, so have to try to open it and catch exception instead  StorageFile storageFile = null;  bool fileExists = false;  try    {    // Try to open file using URI    storageFile = await StorageFile.GetFileFromApplicationUriAsync(                    new Uri("ms-appdata:///local/Myfile.store"));    fileExists = true;  }  catch (FileNotFoundException)  {    fileExists = false;  }  if (!fileExists)   {    await ApplicationData.Current.LocalFolder.CreateFileAsync("Myfile.store",CreationCollisionOption.FailIfExists);   }  ...
复制代码

Windows 8 与 Windows Phone 8 兼容性:

  • WP8下所有的数据存储用LocalFolder(等效于WP7.1下的IsolatedStorage)
  • 不支持漫游数据:ApplicationData.Current.RoamingFolder
  • 临时数据:ApplicationData.Current.RoamingFolder
  • 本地键值对:ApplicationData.Current.LocalSettings
  • 漫游键值对:ApplicationData.Current.RoamingSettings
  • 在Windows8下,可以在用下以代码在XAML元素里加载AppPackages里的图片:
    RecipeImage.Source = new System.Windows.Media.Imaging.BitmapImage(           
    new Uri(@"ms-appx:///Images/french/French_1_600_C.jpg", UriKind.RelativeOrAbsolute));

    在Windows Phone8下,不支持这个URI的语法。只能像Windows Phone7.1那样:

    RecipeImage.Source = new System.Windows.Media.Imaging.BitmapImage("/Images/french/French_1_600_C.jpg");

     

本地文件夹:所有读写的I/O操作仅限于本地文件夹(Local Folder)

 保留文件夹:除一般的存储之外,本地文件夹还用于一些特殊场景:

  • Shared\Media,显示后台播放音乐的艺术家图片。
  • Shared\ShellContent,Tiles的背景图片可以存在这。
  • Shared\Transfer,后台文件传输的存储区域。


数据的序列化:有关数据的持久化

在启动时,从独立存储反序列化。
休眠和墓碑时,序列化并持久存储到独立存储。
激活时,从独立存储反序列化。
终止时,序列化并持久存储到独立存储。

为什么序列化:

序列化可以让内存中的数据集持久存储到文件中,反序列化则可以将文件中的数据读取出来。可以用以下方式做序列化:

  • XmlSerializer
  • DataContractSerializer
  • DataContractJsonSerializer
  • json.net等第三方工具

 DataContractSerializer做序列化:

复制代码
public class MyDataSerializer<TheDataType>    {        public static async Task SaveObjectsAsync(TheDataType sourceData, String targetFileName)        {            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(                targetFileName, CreationCollisionOption.ReplaceExisting);            var outStream = await file.OpenStreamForWriteAsync();            DataContractSerializer serializer = new DataContractSerializer(typeof(TheDataType));            serializer.WriteObject(outStream, sourceData);            await outStream.FlushAsync();            outStream.Close();        }        ...    }
复制代码

用的时候:

List<MyDataObjects> myObjects = ...     await MyDataSerializer<List<MyDataObjects>>.SaveObjectsAsync(myObjects, "MySerializedObjects.xml");

DataContractSerializer反序列化:

复制代码
public class MyDataSerializer<TheDataType>    {        public static async Task<TheDataType> RestoreObjectsAsync(string fileName)        {            StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);            var inStream = await file.OpenStreamForReadAsync();            // Deserialize the objects.             DataContractSerializer serializer =                new DataContractSerializer(typeof(TheDataType));            TheDataType data = (TheDataType)serializer.ReadObject(inStream);            inStream.Close();            return data;        }        ...    }
复制代码

用的时候:

List<MyDataObjects> myObjects    = await MyDataSerializer<List<MyDataObjects>>.RestoreObjectsAsync("MySerializedObjects.xml");

SD卡存储:必须先在application manifest文件中钩选ID_CAP_REMOVABLE_STORAGE,但不能写文件进去,且只能读取APP注册了的文件关联类型。

声明文件类型关联:WMAppManifest.xml文件中,在Extensions下添加一个FileTypeAssociation元素,Extensions必须紧张着Token元素,且FiteType的ContentType属性是必须的。

复制代码
<Extensions>  <FileTypeAssociation Name=“foo" TaskID="_default" NavUriFragment="fileToken=%s">   
 <SupportedFileTypes>      <FileType ContentType="application/foo">.foo</FileType>    </SupportedFileTypes>  </FileTypeAssociation></Extensions>
复制代码

读取SD卡的API:

 


配额管理:Windows Phone里没有配额。应用自己必须小心使用空间。除非需要,否则不要使用存储空间,而且要告诉用户用了多少。定时删除不用的内容,并考虑同步或归档数据到云服务上。

同步和线程:当状态信息是复杂的对象时,可以简单地序列化这个对象,序列化可能会比较慢。将加载和保存数据放在一个单独的线程里,以保证程序的可响应性。

 

转自:http://www.cnblogs.com/icuit/archive/2012/12/09/2799348.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
有道云笔记是一款文件管理和云存储平台,用户可以将各种文件上传至云端进行保存和管理。针对有道云笔记的渗透测试的文件上传核题,主要是测试目标是否存在文件上传漏洞。 文件上传漏洞是指攻击者通过上传恶意文件或者利用上传功能绕过限制,成功在目标服务器上执行任意代码或者获取未授权的访问权限。攻击者可通过上传特定类型的文件,利用后台执行逻辑漏洞或者文件解析漏洞,从而实现对目标系统的攻击或控制。 针对有道云笔记的文件上传漏洞,常见的测试方法包括: 1. 尝试上传各种类型的文件:测试能否上传系统可执行文件、脚本文件或者危险的文件类型。 2. 绕过后台验证:尝试修改请求报文、绕过文件类型检查、篡改上传文件路径等,测试服务器是否能正确地执行上传操作的检查。 3. 文件解析漏洞测试:测试上传的文件是否能够被服务器直接解析,并且触发对应的解析漏洞。 4. 文件重命名与遍历:测试能否修改上传文件文件名,并尝试通过../等目录遍历操作访问到其他敏感文件。 针对发现的漏洞和问题,需要将测试结果整理成详细的渗透测试报告,包括漏洞描述、危害程度评估和修复建议等。然后与有道云笔记的开发团队和管理员进行沟通,提供测试结果和修复建议,并跟踪漏洞修复进展。 及时发现和修复文件上传漏洞,对于保护用户数据和防止潜在的攻击十分重要。因此,有道云笔记应该充分重视渗透测试的文件上传核题,加强安全意识和漏洞修复的流程,确保用户数据的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值