將它們序列化爲xml文件。這是一個通用的例子,如何做到這一點。 請檢查DataContract
here
C#
private static T ReadXmlFile<T>(string path) where T : class
{
T result = null;
if (File.Exists(path))
{
try
{
using (XmlReader reader = XmlReader.Create(path))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
result = (T)serializer.ReadObject(reader);
}
}
catch (Exception ex)
{
throw ex; // or what ever
}
}
return result;
}
private static void WriteXmlFile<T>(string path, T content2write) where T : class
{
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
using (XmlWriter writer = XmlWriter.Create(path,
new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
Encoding = Encoding.UTF8,
CloseOutput = true
}))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(writer, content2write);
}
}
也許將它們保存在自己的AppData
-folder與Environment.SpecialFolder.LocalApplicationData
;-)去這樣
private static readonly string MyPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"MyApp\AppDescription");