你有几个选择。如果您需要以人类可读的格式存储文件,您可以使用 XML 或 JSON 序列化器/反序列化器。这是一个 XML 示例
public static void Serialize(Structure[] input)
{
var serializer = new XmlSerializer(input.GetType());
var sw= new StreamWriter(@"C:\array.ext");
serializer.Serialize(sw, input);
sw.Close();
}
public static Structure[] Deserialize()
{
var stream = new StreamReader(@"C:\array.ext");
var ser = new XmlSerializer(typeof(Structure[]));
object obj = ser.Deserialize(stream);
stream.Close();
return (Structure[])obj;
}
如果你想使用二进制序列化器
public static void Serialize(Structure[] input)
{
var stream = new StreamWriter(@"C:\Array.ext");
var bformatter = new BinaryFormatter();
bformatter.Serialize(stream, input);
stream.Close();
}
public static Structure[] Deserialize()
{
var stream = new StreamReader(@"C:\array.ext");
var bformatter = new BinaryFormatter();
var obj = bformatter.Deserialize(stream);
stream.Close();
return (Structure[])object;
}
您还需要标记您的[Serializable]
课程
[Serializable]
public class Structure { //etc