雖然已經 21 世紀了,我也不知道為何業主非常喜歡 .ICS 這東西
不過,沒辦法收費辦事情,之前,我是自己大概看一下規格後自己產生,
但是最近客戶要做一些改動需求,不能夠這麼單幹了,查詢了一下,有一套一直有在更新的套件 Ical.Net
解釋一下案例,因為只是測試,所以我暫定,就是我程式執行時間的一周後,產生一個事件,十分鐘 後結束
並且再開始前 15 分鐘提醒。
1. Nuget 下載套件 ,我看了一下一直都有再更新升級到 .NET 6 支援
https://www.nuget.org/packages/Ical.Net
2. 接下來就是程式碼的部分
var newEvent = DateTime.Now.AddDays(7);
var title = " XXX 工作室預約";
var evt = new Ical.Net.CalendarComponents.CalendarEvent
{
DtStart = new Ical.Net.DataTypes.CalDateTime(newEvent, "Asia/Taipei"),
Start= new Ical.Net.DataTypes.CalDateTime(newEvent, "Asia/Taipei"),
//事件在十分鐘後結束
DtEnd = new Ical.Net.DataTypes.CalDateTime(newEvent.AddMinutes(10), "Asia/Taipei"),
End = new Ical.Net.DataTypes.CalDateTime(newEvent.AddMinutes(10), "Asia/Taipei"),
Location = "台北市中正區中山南路21號",
Description = "親,這是您的預約 ,別忘記了",
IsAllDay = false,
Summary = title
};
//開始前15分鐘提醒
var alarm = new Ical.Net.CalendarComponents.Alarm()
{
Summary = title,
Trigger = new Ical.Net.DataTypes.Trigger(TimeSpan.FromMinutes(-15)),
Action = Ical.Net.AlarmAction.Display
};
var calendar = new Ical.Net.Calendar();
calendar.Properties.Add(new Ical.Net.CalendarProperty("X-WR-CALNAME", title));
calendar.AddTimeZone("Asia/Taipei");
calendar.Events.Add(evt);
evt.Alarms.Add(alarm);
var serializer = new Ical.Net.Serialization.CalendarSerializer();
var serializedCalendar = serializer.SerializeToString(calendar);
這裡面 注意 event 中的 Summary 一定要寫,因為我用 outlook 的行事曆,如果沒有寫就不會出現任何訊息
非常的奇怪
測試引入 outlook.com 行事曆後

3. 關於時區得部分可以參考這篇文章
https://unicode-org.github.io/cldr-staging/charts/37/supplemental/zone_tzid.html
台灣的是 Asia/Taipei

大概就是這樣,不過因為 summary 問題讓我一直測不出來,之後配合這一篇
https://blog.no2don.com/2023/01/c-aspnet-core-60-wwwroot.html
你就可以做出 下載 .ics 的行事曆預約事件
reference:
https://icalendar.org/RFC-Specifications/iCalendar-RFC-5545/
https://unicode-org.github.io/cldr-staging/charts/37/supplemental/zone_tzid.html
https://github.com/rianjs/ical.net