第一个是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;
}
}
}