运用 DataContractSerializer 存储本地对象
public void Save(string filename,State state) { DataContractSerializer ds = new DataContractSerializer(typeof(State)); using (FileStream fs = new FileStream(GetLocalTempdataPath(filename), FileMode.Create)) { ds.WriteObject(fs, state); fs.Close(); } } public State Load(string filename) { try { DataContractSerializer ds = new DataContractSerializer(typeof(State)); FileStream fs = new FileStream(GetLocalTempdataPath(filename), FileMode.OpenOrCreate); var result = (State)ds.ReadObject(fs); fs.Close(); return result; } catch (Exception) { return null; } } private string GetLocalTempdataPath(string filename) { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}.dat", filename)); } [DataContract] public class State { [DataMember] public string Id{ get; set; } }