如何在关闭程序时保存对象列表,并在再次打开时加载它?
How do I save an object list when I close the program, and load it when I open it again?
如何在关闭程序时将对象列表另存为XML,并在再次打开程序时加载它?
这是我的测试代码,其中包含我要保存的对象列表,然后在我再次打开程序时加载:
public class HighScore
{
public string name;
public int points;
public HighScore(string N, int P)
{
this.name = N;
this.points = P;
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string name;
public static int points;
public static List<HighScore> scorem = new List<HighScore>();
private void Form1_Load(object sender, EventArgs e)
{
scorem.Add(new HighScore("Paul", 20));
scorem.Add(new HighScore("Robert", 30));
scorem.Add(new HighScore("John", 35));
scorem.Add(new HighScore("Steph", 25));
scorem.Add(new HighScore("Seth", 40));
scorem.Add(new HighScore("Johnny", 55));
scorem.Add(new HighScore("Michael", 200));
scorem.Add(new HighScore("Robertinoe", 300));
scorem.Add(new HighScore("Marstrand", 2500));
scorem.Add(new HighScore("Doe", 3000));
scorem = scorem.OrderByDescending(x => x.points).ToList();
foreach(HighScore per in scorem)
{
label1.Text += per.name + " " + per.points + "\n";
}
}
一种易于实现的方法是使用 binaryformatter.
进行序列化
将 [Serializable]
属性添加到您的高分 class:
[Serializable]
public class HighScore
{
public string name;
public int points;
public HighScore(string N, int P)
{
this.name = N;
this.points = P;
}
为了保存您的数据,您序列化了 List(它也具有 Serializable 属性)
BinaryFormatter.Serialize(somestream,somehighsscorelist)
并再次取回它:
List<HighScore> savedscores = (List<HighScore>)BinaryFormatter.Deserialize(somestream)
此外,我会将文件名存储为应用程序设置,请记住,如果您更改 HighScore class 的结构,您将无法反序列化旧文件。 XML 序列化会更容忍版本。
这是一个完整的例子:
[Serializable]
public class HighScore {
public string name;
public int points;
public HighScore(string N, int P) {
this.name = N;
this.points = P;
}
}
[Serializable]
public class GameData {
public List<HighScore> ScoreList { get; set; }
public GameData() {
ScoreList = new List<HighScore>();
}
}
private GameData gameData = new GameData();
private void load_Click(object sender, RoutedEventArgs e) {
Stream stream = null;
try {
stream = File.Open("file.bin", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
gameData = (GameData)bformatter.Deserialize(stream);
} finally {
if (stream != null)
stream.Close();
}
}
private void save_Click(object sender, RoutedEventArgs e) {
Stream stream = null;
try {
stream = File.Open("file.bin", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, gameData);
stream.Close();
} finally {
if (stream != null)
stream.Close();
}
}
private void display_Click(object sender, RoutedEventArgs e) {
foreach (HighScore per in gameData.ScoreList) {
Console.WriteLine(per.name + " " + per.points + "\n");
}
}
private void addscore_Click(object sender, RoutedEventArgs e) {
gameData.ScoreList.Add(new HighScore("Doe", 3000));
}
如何在关闭程序时将对象列表另存为XML,并在再次打开程序时加载它?
这是我的测试代码,其中包含我要保存的对象列表,然后在我再次打开程序时加载:
public class HighScore
{
public string name;
public int points;
public HighScore(string N, int P)
{
this.name = N;
this.points = P;
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string name;
public static int points;
public static List<HighScore> scorem = new List<HighScore>();
private void Form1_Load(object sender, EventArgs e)
{
scorem.Add(new HighScore("Paul", 20));
scorem.Add(new HighScore("Robert", 30));
scorem.Add(new HighScore("John", 35));
scorem.Add(new HighScore("Steph", 25));
scorem.Add(new HighScore("Seth", 40));
scorem.Add(new HighScore("Johnny", 55));
scorem.Add(new HighScore("Michael", 200));
scorem.Add(new HighScore("Robertinoe", 300));
scorem.Add(new HighScore("Marstrand", 2500));
scorem.Add(new HighScore("Doe", 3000));
scorem = scorem.OrderByDescending(x => x.points).ToList();
foreach(HighScore per in scorem)
{
label1.Text += per.name + " " + per.points + "\n";
}
}
一种易于实现的方法是使用 binaryformatter.
进行序列化将 [Serializable]
属性添加到您的高分 class:
[Serializable]
public class HighScore
{
public string name;
public int points;
public HighScore(string N, int P)
{
this.name = N;
this.points = P;
}
为了保存您的数据,您序列化了 List
BinaryFormatter.Serialize(somestream,somehighsscorelist)
并再次取回它:
List<HighScore> savedscores = (List<HighScore>)BinaryFormatter.Deserialize(somestream)
此外,我会将文件名存储为应用程序设置,请记住,如果您更改 HighScore class 的结构,您将无法反序列化旧文件。 XML 序列化会更容忍版本。
这是一个完整的例子:
[Serializable]
public class HighScore {
public string name;
public int points;
public HighScore(string N, int P) {
this.name = N;
this.points = P;
}
}
[Serializable]
public class GameData {
public List<HighScore> ScoreList { get; set; }
public GameData() {
ScoreList = new List<HighScore>();
}
}
private GameData gameData = new GameData();
private void load_Click(object sender, RoutedEventArgs e) {
Stream stream = null;
try {
stream = File.Open("file.bin", FileMode.Open);
BinaryFormatter bformatter = new BinaryFormatter();
gameData = (GameData)bformatter.Deserialize(stream);
} finally {
if (stream != null)
stream.Close();
}
}
private void save_Click(object sender, RoutedEventArgs e) {
Stream stream = null;
try {
stream = File.Open("file.bin", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, gameData);
stream.Close();
} finally {
if (stream != null)
stream.Close();
}
}
private void display_Click(object sender, RoutedEventArgs e) {
foreach (HighScore per in gameData.ScoreList) {
Console.WriteLine(per.name + " " + per.points + "\n");
}
}
private void addscore_Click(object sender, RoutedEventArgs e) {
gameData.ScoreList.Add(new HighScore("Doe", 3000));
}