wp8开发--IsolatedStorageSettings存储数据和IsolatedStorageFileHelper类

第一个是IsolatedStorageSettingHelper工具类,专门用来存储数据和删除数据的!
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.IO.IsolatedStorage;
namespace WP8Demo.IsolatedStorageCommon
{
    public class IsolatedStorageSettingHelper
    {
        /// <summary>
        /// Add IsolateStorage Setting
        /// </summary>
        /// <param name="key">Key</param>
        /// <param name="value">Value</param>
        /// <returns>Is Add</returns>
        public static bool AddIsolateStorageObj(string key, object value)
        {
            bool isAdd = false;
            if (!string.IsNullOrEmpty(key) && value != null)
            {
                IsolatedStorageSettings.ApplicationSettings[key] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                isAdd = true;
            }
            return isAdd;
        }


        /// <summary>
        /// Get IsolateStorage Save Value To String Format
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>String Format</returns>
        public static string GetIsolateStorageByStr(string key)
        {
            string storeStr = string.Empty;
            if (!string.IsNullOrEmpty(key))
            {
                if (IsolateStorageKeyIsExist(key))
                    storeStr = IsolatedStorageSettings.ApplicationSettings[key].ToString();
            }
            return storeStr;
        }


        /// <summary>
        /// Get IsolateStorage Save Value To Object Format
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>object Format</returns>
        public static object GetIsolateStorageByObj(string key)
        {
            object storeObj = null;
            if (!string.IsNullOrEmpty(key))
            {
                if (IsolateStorageKeyIsExist(key))
                    storeObj = IsolatedStorageSettings.ApplicationSettings[key];
            }
            return storeObj;
        }


        /// <summary>
        /// IsolateStorage Is Exists
        /// </summary>
        /// <param name="key">Storage Key</param>
        /// <returns>Is Exists</returns>
        public static bool IsolateStorageKeyIsExist(string key)
        {
            bool isExist = false;
            if (!string.IsNullOrEmpty(key))
            {
                foreach (string currentkey in IsolatedStorageSettings.ApplicationSettings.Keys)
                {
                    if (currentkey.Equals(key))
                    {
                        isExist = true;
                        break;
                    }
                }
            }
            return isExist;
        }


        /// <summary>
        /// Remove The IsolateStorage KeyValuePair
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>Is Remove</returns>
        public static bool RemoveIsolateStorageByKey(string key)
        {
            bool IsRemove = false;
            if (!string.IsNullOrEmpty(key))
            {
                IsolatedStorageSettings.ApplicationSettings.Remove(key);
                IsolatedStorageSettings.ApplicationSettings.Save();
                IsRemove = true;
            }
            return IsRemove;
        }
    }
}
第二个是IsolatedStorageFileHelper工具类,代码如下:
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace WP8Demo.IsolatedStorageCommon
{
    public class IsolatedStorageFileHelper
    {
        private static IsolatedStorageFile _Store = null;
        private static IsolatedStorageFile Store
        {
            get
            {
                if (_Store == null)
                {
                    _Store = IsolatedStorageFile.GetUserStoreForApplication();
                }
                return _Store;
            }
        }
        public static void CreateFolder(string dir)
        {
            if (Store.DirectoryExists(dir))
                return;
            Store.CreateDirectory(dir);
        }
        public static void DeleteFolder(string dir)
        {
            if (Store.DirectoryExists(dir))
                return;
            Store.DeleteDirectory(dir);
        }

        public static void DeleteFile(string path)
        {
            if (!Store.FileExists(path))
                return;
            Store.DeleteFile(path);
        }

        public static IsolatedStorageFileStream CreateFile(string path)
        {
            string[] parts = path.Split('\\');
            string dir = "";
            for (int i = 0; i < parts.Length - 1; i++)
            {
                string part = parts[i];
                dir = System.IO.Path.Combine(dir, part);
                if (!Store.DirectoryExists(dir))
                    Store.CreateDirectory(dir);
            }
            return Store.CreateFile(path);
        }

        public static bool FileExists(string path)
        {
            return Store.FileExists(path);
        }

        public static IsolatedStorageFileStream OpenFile(string path)
        {
            return Store.OpenFile(path, System.IO.FileMode.Open);
        }

        public static void SerializeObject(object obj, string path)
        {
            if (Store.FileExists(path))
                Store.DeleteFile(path);
            XmlSerializer xml = new XmlSerializer(obj.GetType());
            using (var stream = CreateFile(path))
            {
                xml.Serialize(stream, obj);
            }
        }

        public static T DeSerializeObject<T>(string path)
        {
            if (!Store.FileExists(path))
                return default(T);
            XmlSerializer xml = new XmlSerializer(typeof(T));
            object obj = null;
            using (var stream = OpenFile(path))
            {
                obj = xml.Deserialize(stream);
            }
            return (T)obj;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值