Load a Calendar in C# and VB.NET

Gembox.Email allows you to load calendars from a file or a stream in iCalendar (ics) format. The loaded calendar can be modified, saved, or added to a MailMessage.

The following example shows how to load a calendar from a file and display events, tasks, and reminders, and their most common properties, on a console window.

Screenshot
Load Screenshot

See the full code below, use Run Example to execute.

Upload your file(Drag files here)

Download an input file

using GemBox.Email;
using GemBox.Email.Calendar;
using System;
using System.Linq;
using System.Text;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Load calendar
        Calendar calendar = Calendar.Load("Simple.ics");
        StringBuilder sb = new StringBuilder();

        // Write a calendar method specifying what the calendar is used for.
        sb.AppendLine(string.Format("Method: {0}", calendar.Method));

        sb.AppendLine(string.Format("Count of events: {0}", calendar.Events.Count));
        sb.AppendLine(string.Format("Count of tasks: {0}", calendar.Tasks.Count));

        // Iterate through all events.
        foreach (Event e in calendar.Events)
        {
            sb.AppendLine("---------EVENT----------");
            sb.AppendLine(string.Format("Organizer: {0}", e.Organizer));
            sb.AppendLine(string.Format("Name: {0}", e.Name));
            sb.AppendLine(string.Format("Start: {0:G}", e.Start));
            sb.AppendLine(string.Format("End: {0:G}", e.End));

            sb.AppendLine(string.Format("Count of reminders: {0}", e.Reminders.Count));

            // Iterate through all reminders.
            foreach (Reminder reminder in e.Reminders)
            {
                sb.AppendLine("  --------REMINDER---------");
                sb.AppendLine(string.Format("  Action: {0}", reminder.ReminderAction));
                sb.AppendLine(string.Format("  Days to trigger before start: {0}", reminder.TriggerBeforeStart?.Days));
            }
        }

        // Iterate through all tasks.
        foreach (Task task in calendar.Tasks)
        {
            sb.AppendLine("---------TASK-----------");
            sb.AppendLine(string.Format("Organizer: {0}", task.Organizer));
            sb.AppendLine(string.Format("Name: {0}", task.Name));
            sb.AppendLine(string.Format("Start: {0:G}", task.Start));
            sb.AppendLine(string.Format("Deadline: {0:G}", task.Deadline));

            sb.AppendLine(string.Format("Count of reminders: {0}", task.Reminders.Count));

            // Iterate through all reminders.
            foreach (Reminder reminder in task.Reminders)
            {
                sb.AppendLine("  --------REMINDER---------");
                sb.AppendLine(string.Format("  Action: {0}", reminder.ReminderAction));
                sb.AppendLine(string.Format("  Days to trigger before start: {0}", reminder.TriggerBeforeStart?.Days));
            }
        }

        Console.WriteLine(sb.ToString());

    }
}
Imports System
Imports System.Text
Imports GemBox.Email
Imports GemBox.Email.Calendar

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Load calendar
        Dim calendar as Calendar = Calendar.Load("Simple.ics")
        Dim sb As StringBuilder = New StringBuilder

        ' Write a calendar method specifying what is the calendar used for.
        sb.AppendLine(String.Format("Method: {0}", calendar.Method))
        sb.AppendLine(String.Format("Count of events: {0}", calendar.Events.Count))
        sb.AppendLine(String.Format("Count of tasks: {0}", calendar.Tasks.Count))

        ' Iterate through all events.
        For Each e As [Event] In calendar.Events
            sb.AppendLine("---------EVENT----------")
            sb.AppendLine(String.Format("Organizer: {0}", e.Organizer))
            sb.AppendLine(String.Format("Name: {0}", e.Name))
            sb.AppendLine(String.Format("Start: {0:G}", e.Start))
            sb.AppendLine(String.Format("End: {0:G}", e.End))
            sb.AppendLine(String.Format("Count of reminders: {0}", e.Reminders.Count))

            ' Iterate through all reminders.
            For Each reminder As Reminder In e.Reminders
                sb.AppendLine("  --------REMINDER---------")
                sb.AppendLine(String.Format("  Action: {0}", reminder.ReminderAction))
                sb.AppendLine(String.Format("  Days to trigger before start: {0}", reminder.TriggerBeforeStart?.Days))
            Next
        Next

        ' Iterate through all tasks.
        For Each task As Task In calendar.Tasks
            sb.AppendLine("---------TASK-----------")
            sb.AppendLine(String.Format("Organizer: {0}", task.Organizer))
            sb.AppendLine(String.Format("Name: {0}", task.Name))
            sb.AppendLine(String.Format("Start: {0:G}", task.Start))
            sb.AppendLine(String.Format("Deadline: {0:G}", task.Deadline))
            sb.AppendLine(String.Format("Count of reminders: {0}", task.Reminders.Count))
        
            ' Iterate through all reminders
            For Each reminder As Reminder In task.Reminders
                sb.AppendLine("  --------REMINDER---------")
                sb.AppendLine(String.Format("  Action: {0}", reminder.ReminderAction))
                sb.AppendLine(String.Format("  Days to trigger before start: {0}", reminder.TriggerBeforeStart?.Days))
            Next
        Next

        Console.WriteLine(sb.ToString)

    End Sub

End Module