Creating iCal Files in c#

前端 未结 9 1625
日久生厌
日久生厌 2020-11-29 17:47

I\'m looking for a good method of generating an iCalendar file (*.ics) in c# (asp.net). I\'ve found a couple resources, but one thing that has been lacking is their support

相关标签:
9条回答
  • 2020-11-29 17:53

    Check out http://www.codeproject.com/KB/vb/vcalendar.aspx

    It doesn't handle the quoted-printable fields like you asked, but the rest of the code is there and can be modified.

    0 讨论(0)
  • 2020-11-29 17:56

    i know it is too late, but it may help others. in my case i wrote following text file with .ics extension

    BEGIN:VCALENDAR
    VERSION:2.0
    PRODID:-//Calendly//EN
    CALSCALE:GREGORIAN
    METHOD:PUBLISH
    BEGIN:VEVENT
    DTSTAMP:20170509T164109Z
    UID:your id-11273661
    DTSTART:20170509T190000Z
    DTEND:20170509T191500Z
    CLASS:PRIVATE
    DESCRIPTION:Event Name: 15 Minute Meeting\nDate & Time: 03:00pm - 03:15pm (
     Eastern Time - US & Canada) on Tuesday\, May 9\, 2017\n\nBest Phone Number
      To Reach You :: xxxxxxxxx\n\nany "link": https://wwww.yahoo.com\n\n
    SUMMARY:15 Minute Meeting
    TRANSP:OPAQUE
    END:VEVENT
    END:VCALENDAR
    

    it worked for me.

    0 讨论(0)
  • 2020-11-29 17:57

    I'm missing an example with custom time zones. So here a snippet that show how you can set a time zone in the ics (and send it to the browser in asp.net).

    //set a couple of variables for demo purposes
    DateTime IcsDateStart = DateTime.Now.AddDays(2);
    DateTime IcsDateEnd = IcsDateStart.AddMinutes(90);
    string IcsSummary = "ASP.Net demo snippet";
    string IcsLocation = "Amsterdam (Netherlands)";
    string IcsDescription = @"This snippes show you how to create a calendar item file (.ics) in ASP.NET.\nMay it be useful for you.";
    string IcsFileName = "MyCalendarFile";
    
    //create a new stringbuilder instance
    StringBuilder sb = new StringBuilder();
    
    //begin the calendar item
    sb.AppendLine("BEGIN:VCALENDAR");
    sb.AppendLine("VERSION:2.0");
    sb.AppendLine("PRODID:stackoverflow.com");
    sb.AppendLine("CALSCALE:GREGORIAN");
    sb.AppendLine("METHOD:PUBLISH");
    
    //create a custom time zone if needed, TZID to be used in the event itself
    sb.AppendLine("BEGIN:VTIMEZONE");
    sb.AppendLine("TZID:Europe/Amsterdam");
    sb.AppendLine("BEGIN:STANDARD");
    sb.AppendLine("TZOFFSETTO:+0100");
    sb.AppendLine("TZOFFSETFROM:+0100");
    sb.AppendLine("END:STANDARD");
    sb.AppendLine("END:VTIMEZONE");
    
    //add the event
    sb.AppendLine("BEGIN:VEVENT");
    
    //with a time zone specified
    sb.AppendLine("DTSTART;TZID=Europe/Amsterdam:" + IcsDateStart.ToString("yyyyMMddTHHmm00"));
    sb.AppendLine("DTEND;TZID=Europe/Amsterdam:" + IcsDateEnd.ToString("yyyyMMddTHHmm00"));
    
    //or without a time zone
    //sb.AppendLine("DTSTART:" + IcsDateStart.ToString("yyyyMMddTHHmm00"));
    //sb.AppendLine("DTEND:" + IcsDateEnd.ToString("yyyyMMddTHHmm00"));
    
    //contents of the calendar item
    sb.AppendLine("SUMMARY:" + IcsSummary + "");
    sb.AppendLine("LOCATION:" + IcsLocation + "");
    sb.AppendLine("DESCRIPTION:" + IcsDescription + "");
    sb.AppendLine("PRIORITY:3");
    sb.AppendLine("END:VEVENT");
    
    //close calendar item
    sb.AppendLine("END:VCALENDAR");
    
    //create a string from the stringbuilder
    string CalendarItemAsString = sb.ToString();
    
    //send the ics file to the browser
    Response.ClearHeaders();
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "text/calendar";
    Response.AddHeader("content-length", CalendarItemAsString.Length.ToString());
    Response.AddHeader("content-disposition", "attachment; filename=\"" + IcsFileName + ".ics\"");
    Response.Write(CalendarItemAsString);
    Response.Flush();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    
    0 讨论(0)
  • 2020-11-29 18:00

    I wrote a shim function to handle this. It's mostly compliant--the only hangup is that the first line is 74 characters instead of 75 (the 74 is to handle the space on subsequent lines)...

     Private Function RFC2445TextField(ByVal LongText As String) As String
    
         LongText = LongText.Replace("\", "\\")
         LongText = LongText.Replace(";", "\;")
         LongText = LongText.Replace(",", "\,")
    
         Dim sBuilder As New StringBuilder
         Dim charArray() As Char = LongText.ToCharArray
    
         For i = 1 To charArray.Length
             sBuilder.Append(charArray(i - 1))
             If i Mod 74 = 0 Then sBuilder.Append(vbCrLf & " ")
         Next
    
         Return sBuilder.ToString
    
     End Function
    

    I use this for the summary and description on our ICS feed. Just feed the line with the field already prepended (e.g. LongText = "SUMMARY:Event Title"). As long as you set caching decently long, it's not too expensive of an operation.

    0 讨论(0)
  • 2020-11-29 18:05

    According to RFC-2445, the comment and description fields are TEXT. The rules for a test field are: [1] A single line in a TEXT field is not to exceed 75 octets. [2] Wrapping is achieved by inserting a CRLF followed by whitespace. [3] There are several characters that must be encoded including \ (reverse slash) ; (semicolon) , (comma) and newline. Using a \ (reverse slash) as a delimiter gives \ \; \, \n

    Example: The following is an example of the property with formatted line breaks in the property value:

     DESCRIPTION:Meeting to provide technical review for "Phoenix"
       design.\n Happy Face Conference Room. Phoenix design team
       MUST attend this meeting.\n RSVP to team leader.
    
    0 讨论(0)
  • 2020-11-29 18:06

    iCal (ical 2.0) and quoted-printable don't go together.

    Quoted-printable is used a lot in vCal (vCal 1.0) to represent non-printable characters, e.g. line-breaks (=0D=0A). The default vCal encoding is 7-bit, so sometimes you need to use quoted-printable to represent non-ASCII characters (you can override the default encoding, but the other vCal-compliant communicating party is not required to understand it.)

    In iCal, special characters are represented using escapes, e.g. '\n'. The default encoding is UTF-8, all iCal-compliant parties must support it and that makes quoted-printable completely unnecessary in iCal 2.0 (and vCard 3.0, for that matter).

    You may need to back your customer/stakeholder to clarify the requirements. There seems to be confusion between vCal and iCal.

    0 讨论(0)
提交回复
热议问题