将数组列表存储在隔离存储中

本文关键字:存储 隔离 列表 数组 | 更新日期: 2023-09-27 18:36:22

如何将数组列表存储到独立存储中?也可以将图像放置在数组列表中吗?谢谢

将数组列表存储在隔离存储中

就像评论所说的那样,您所需要的只是获取一些可序列化的对象,并且能够将其存储在IS中。请注意,多个维度的数组不可序列化!

这是我用于IS的代码块:

using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
namespace PhoneApp1
{
public class IsolatedStorage
{
    public static void SaveToIs(String fileName, Object saved)
    {
        try
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isf.FileExists(fileName))
                {
                    isf.DeleteFile(fileName);
                }

                using (IsolatedStorageFileStream fs = isf.CreateFile(fileName))
                {
                    XmlSerializer ser = new XmlSerializer(saved.GetType());
                    ser.Serialize(fs, saved);
                }
            }
        }
        catch (IsolatedStorageException ex)
        {
            MessageBox.Show(ex.Message);
        }

    }
    public static Object loadFromIS(String fileName, Type t)
    {
        Object result = null;
        try
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (isf.FileExists(fileName))
                {
                    using (StreamReader sr = new StreamReader(isf.OpenFile(fileName, FileMode.Open)))
                    {
                        XmlSerializer ser = new XmlSerializer(t);
                        result = ser.Deserialize(sr);
                    }
                }
            }
        }
        catch (IsolatedStorageException ex)
        {
            MessageBox.Show(ex.Message);
        }
        catch (InvalidOperationException e)
        {
            MessageBox.Show(e.Message);
        }
        return result;
    }
}
}