Search code examples
c#.neticalendar

How to extract data using Ical.Net?


I have an ics file I want to read data out from. I downloaded Ical.Net using Nuget and I'm trying to figure out how to work with it. All I want to do is get the same info as I would get if I uploaded my ics file to this website: https://icsconvert.appspot.com/

Which is, for each "event":

  • Summary
  • Description
  • Start
  • End
  • Location

But I don't understand how I can get that info out of the file. Here's what I have so far:

string filepath = @"C:\My\file\path\myIcs.ics";
System.IO.StreamReader file = new System.IO.StreamReader(filepath);
string content = file.ReadToEnd();
file.Close();

Ical.Net.Calendar calendar = Ical.Net.Calendar.Load(content);

foreach (var c in calendar.RecurringItems)
{
    // 1. Do something!
    // 2. ????
    // 3. Profit!
}

Here's a sample file I got from here:

BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
SUMMARY:Access-A-Ride Pickup
DTSTART;TZID=America/New_York:20130802T103400
DTEND;TZID=America/New_York:20130802T110400
LOCATION:1000 Broadway Ave.\, Brooklyn
DESCRIPTION: Access-A-Ride trip to 900 Jay St.\, Brooklyn
STATUS:CONFIRMED
SEQUENCE:3
BEGIN:VALARM
TRIGGER:-PT10M
DESCRIPTION:Pickup Reminder
ACTION:DISPLAY
END:VALARM
END:VEVENT
BEGIN:VEVENT
SUMMARY:Access-A-Ride Pickup
DTSTART;TZID=America/New_York:20130802T200000
DTEND;TZID=America/New_York:20130802T203000
LOCATION:900 Jay St.\, Brooklyn
DESCRIPTION: Access-A-Ride trip to 1000 Broadway Ave.\, Brooklyn
STATUS:CONFIRMED
SEQUENCE:3
BEGIN:VALARM
TRIGGER:-PT10M
DESCRIPTION:Pickup Reminder
ACTION:DISPLAY
END:VALARM
END:VEVENT
END:VCALENDAR

How can I get the data I want? I want to use the foreach to save that data into a custom list.


Solution

  • As far as I can see you just have a set of two separate events there, not recurring events (as in defined with an RRULE).

    I think you would likely find what you need in calendar.Events instead (I've never used this library, just had a quick poke at the source code and it looks like the calendar class has an "Events" property).