C# - 将数据保存和加载到文件
C# - Saving and Loading data to file
我决定开始编码并正在学习 C#,在做了一些小项目之后,我决定进一步提高并制作一个文本冒险游戏,带有保存和加载功能,如果我感到滑稽我会尝试添加一些多人游戏。虽然我并没有因此而真正遇到障碍,但我情不自禁地觉得我做的加载功能真的不是最理想的。 save还行,感觉对我有用,但是load感觉真的可以简化,就是不知道用什么好。
我也不太介意,但通过这种方式,如果我添加其他 attributes/skills 或任何其他需要保存的内容,我也必须将所有内容添加到加载函数中,并且会更长。
我尝试在此处、c# 文档和其他网站上进行搜索,但找不到适用于这种情况的解决方案。任何人都可以帮我找到更好的方法吗?或者这是我真正能做的最好的,因为它是不同的数据类型?
编辑:为了简化和阐明我正在搜索的答案,我试图找到一种更简单、更具可扩展性的方法来将数据保存和加载到文件中。
static void LoadGame(CharData PlayerData)
{
Console.WriteLine("Enter the name of the character to load as shown below.");
//getting current directory info, setting to di
DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory());
//need to initialize these outside of a loop
int SaveFiles = 0;
string DisplayName = " ";
int DisplayNameLength = 0;
//looks through files in working directory ending in '.fasv', displays them in format '{x}. John Smith'
foreach (var fi in di.GetFiles("*.fasv"))
{
SaveFiles++;
DisplayNameLength = fi.Name.Length;
//remove .fasv from displayed name to make it look nicer
DisplayName = fi.Name.Remove(DisplayNameLength - 5, 5);
Console.WriteLine(SaveFiles.ToString() + ". " + DisplayName);
}
string toLoad = Console.ReadLine();
using StreamReader sr = new StreamReader(toLoad + ".fasv");
//the name is easy to get since it's a string. but integers...
PlayerData.Name = sr.ReadLine();
//... not so much. i hate all of this and i feel like it's gross, but i don't know how else to do it
int hp, xp, level, toughness, innovation, mind, empathy, spryness;
Int32.TryParse(sr.ReadLine(), out hp);
Int32.TryParse(sr.ReadLine(), out xp);
Int32.TryParse(sr.ReadLine(), out level);
Int32.TryParse(sr.ReadLine(), out toughness);
Int32.TryParse(sr.ReadLine(), out innovation);
Int32.TryParse(sr.ReadLine(), out mind);
Int32.TryParse(sr.ReadLine(), out empathy);
Int32.TryParse(sr.ReadLine(), out spryness);
PlayerData.Health = hp;
PlayerData.Level = level;
PlayerData.XP = xp;
PlayerData.Toughness = toughness;
PlayerData.Innovation = innovation;
PlayerData.Mind = mind;
PlayerData.Empathy = empathy;
PlayerData.Spryness = spryness;
sr.Close();
InGame(PlayerData);
}
static void SaveGame(CharData PlayerData)
{
using (StreamWriter sw = new StreamWriter(PlayerData.Name + ".fasv"))
{
foreach (System.Reflection.PropertyInfo stat in PlayerData.GetType().GetProperties())
{
//write player data properties to file line by line, using stat to iterate through the player data properties
sw.WriteLine(stat.GetValue(PlayerData));
}
sw.Close();
}
}
如果您没有为文件数据设置特定的数据格式,我建议您使用序列化程序,例如 JSON.NET。您可以使用 NuGet 将 newtonsoft.json 添加到您的项目中,这样您就可以执行类似于以下操作的操作:
using (StreamWriter file = File.CreateText(pathToPlayerFile))
{
var serializer = new JsonSerializer();
serializer.Serialize(file, playerData);
}
然后您从文件中读取的代码将非常相似:
using (var file = File.OpenText(pathToPlayerFile))
{
var serializer = new JsonSerializer();
return (CharData)serializer.Deserialize(file, typeof(CharData));
}
我从 newtonsoft.com 那里借用了那些代码片段。 CreateText
将创建(或覆盖)文件并将对象作为 JSON 对象写入。
我决定开始编码并正在学习 C#,在做了一些小项目之后,我决定进一步提高并制作一个文本冒险游戏,带有保存和加载功能,如果我感到滑稽我会尝试添加一些多人游戏。虽然我并没有因此而真正遇到障碍,但我情不自禁地觉得我做的加载功能真的不是最理想的。 save还行,感觉对我有用,但是load感觉真的可以简化,就是不知道用什么好。
我也不太介意,但通过这种方式,如果我添加其他 attributes/skills 或任何其他需要保存的内容,我也必须将所有内容添加到加载函数中,并且会更长。
我尝试在此处、c# 文档和其他网站上进行搜索,但找不到适用于这种情况的解决方案。任何人都可以帮我找到更好的方法吗?或者这是我真正能做的最好的,因为它是不同的数据类型?
编辑:为了简化和阐明我正在搜索的答案,我试图找到一种更简单、更具可扩展性的方法来将数据保存和加载到文件中。
static void LoadGame(CharData PlayerData)
{
Console.WriteLine("Enter the name of the character to load as shown below.");
//getting current directory info, setting to di
DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory());
//need to initialize these outside of a loop
int SaveFiles = 0;
string DisplayName = " ";
int DisplayNameLength = 0;
//looks through files in working directory ending in '.fasv', displays them in format '{x}. John Smith'
foreach (var fi in di.GetFiles("*.fasv"))
{
SaveFiles++;
DisplayNameLength = fi.Name.Length;
//remove .fasv from displayed name to make it look nicer
DisplayName = fi.Name.Remove(DisplayNameLength - 5, 5);
Console.WriteLine(SaveFiles.ToString() + ". " + DisplayName);
}
string toLoad = Console.ReadLine();
using StreamReader sr = new StreamReader(toLoad + ".fasv");
//the name is easy to get since it's a string. but integers...
PlayerData.Name = sr.ReadLine();
//... not so much. i hate all of this and i feel like it's gross, but i don't know how else to do it
int hp, xp, level, toughness, innovation, mind, empathy, spryness;
Int32.TryParse(sr.ReadLine(), out hp);
Int32.TryParse(sr.ReadLine(), out xp);
Int32.TryParse(sr.ReadLine(), out level);
Int32.TryParse(sr.ReadLine(), out toughness);
Int32.TryParse(sr.ReadLine(), out innovation);
Int32.TryParse(sr.ReadLine(), out mind);
Int32.TryParse(sr.ReadLine(), out empathy);
Int32.TryParse(sr.ReadLine(), out spryness);
PlayerData.Health = hp;
PlayerData.Level = level;
PlayerData.XP = xp;
PlayerData.Toughness = toughness;
PlayerData.Innovation = innovation;
PlayerData.Mind = mind;
PlayerData.Empathy = empathy;
PlayerData.Spryness = spryness;
sr.Close();
InGame(PlayerData);
}
static void SaveGame(CharData PlayerData)
{
using (StreamWriter sw = new StreamWriter(PlayerData.Name + ".fasv"))
{
foreach (System.Reflection.PropertyInfo stat in PlayerData.GetType().GetProperties())
{
//write player data properties to file line by line, using stat to iterate through the player data properties
sw.WriteLine(stat.GetValue(PlayerData));
}
sw.Close();
}
}
如果您没有为文件数据设置特定的数据格式,我建议您使用序列化程序,例如 JSON.NET。您可以使用 NuGet 将 newtonsoft.json 添加到您的项目中,这样您就可以执行类似于以下操作的操作:
using (StreamWriter file = File.CreateText(pathToPlayerFile))
{
var serializer = new JsonSerializer();
serializer.Serialize(file, playerData);
}
然后您从文件中读取的代码将非常相似:
using (var file = File.OpenText(pathToPlayerFile))
{
var serializer = new JsonSerializer();
return (CharData)serializer.Deserialize(file, typeof(CharData));
}
我从 newtonsoft.com 那里借用了那些代码片段。 CreateText
将创建(或覆盖)文件并将对象作为 JSON 对象写入。