這個方法可以儲存成自訂的專案檔,方便下一次Load
要先去Json的官網下載函式庫
using Newtonsoft.Json;
List<team> TeamPlayerData = new List<team>();
private void saveButton_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "專案檔(*.proj)|*.proj";
saveFileDialog.ShowDialog();
if (saveFileDialog.FileName != "")
{
string json = JsonConvert.SerializeObject(TeamPlayerData);
//第二個參數設定為true表示不覆蓋原本的內容,把新內容直接添加進去
StreamWriter sw = new StreamWriter(saveFileDialog.FileName, false, Encoding.Unicode);
sw.WriteLine(json);
sw.Close();
}
}
private void loadButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "專案檔(*.proj)|*.proj";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string csPATH = openFileDialog.FileName;
if (File.Exists(csPATH))
{
TeamPlayerData.Clear();
StreamReader sr = new StreamReader(csPATH);
string desJson = sr.ReadToEnd();
sr.Close();
TeamPlayerData = JsonConvert.DeserializeObject<List<Scene_INFO>>(desJson);
}
}
}