這個方法可以儲存成自訂的專案檔,方便下一次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);
         }
     }
}
#C# #Winform







你可能感興趣的文章

Day05 state 及 props 的應用

Day05 state 及 props 的應用

Vue.js 學習旅程Mile 14 – Form Input Bindings 表單綁定篇:v-model

Vue.js 學習旅程Mile 14 – Form Input Bindings 表單綁定篇:v-model

[day00] Oh...開始了......

[day00] Oh...開始了......






留言討論