Storing arraylist in IsolatedStorage

谁说胖子不能爱 提交于 2019-12-13 04:45:36

问题


how do i store a list of array into isolated storage? Possible to place image inside the arraylist too? Thanks


回答1:


Like the comments said all you need is to get some serializable object and you're able to store it in IS. Be awate that arrays of more than one dimension are not serializable!

Here is a code chunk I use for 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;
    }
}
}


来源:https://stackoverflow.com/questions/10919306/storing-arraylist-in-isolatedstorage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!