[C#] 讀取JSON格式,寫入自訂Class

以console winform為例

mail_info.txt 內容 :

{
"Subject":"我是主旨",
"Content":"系統自動發出",
"MailTo":["abc@gmail.com","def@yahoo.com.tw"],
"MailCC": []
}

cmd 指令 :

send_mail.exe <txt file path>

send_mail.exe 內容 :

using System;
using System.IO;
using System.Text.Json;

namespace myNamespace
{
  internal class Program
  {
    static void Main(string[] args)
    {
      string filePath = "";
			
      if (args == null || args.Length == 0)
      {
        Console.WriteLine("where is the mail_info.txt file path?");
        return;
      }
      filePath = args[0].ToString();
            
      string jsonString = File.ReadAllText(filePath); // 讀取 JSON 資料
      Mail_Info _mailInfo = JsonSerializer.Deserialize<Mail_Info>(jsonString);
            
      //todo:send mail action
    }
  }
	
  private class Mail_Info
  {
    public string Subject { get; set; }
    public string Content { get; set; }
    public List<string> MailTo { get; set; }
    public List<string> MailCC { get; set; }
  }
}

註。速記簡化記錄,可能有小bug