[英]C# - Saving and Loading data to file
我決定開始編碼並正在學習 c#,在做了幾個小項目之后,我決定稍微加強一下,制作一個帶有保存和加載功能的文字冒險游戲,如果我覺得很滑稽,我會嘗試添加一些多人游戲。 雖然我並沒有因此而真正遇到障礙,但我不禁覺得我的負載 function 真的是次優的。 保存還不錯,感覺對我有用,但是感覺負載真的可以簡化,就是不知道用什么。
我也不會真的介意,但是通過這種方式,如果我添加其他屬性/技能或任何其他需要保存的內容,我將不得不將所有內容添加到負載 function 中,並且會更長。
我試圖在這里搜索 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
將創建(或覆蓋)文件並將 object 寫入 JSON object。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.