在c#中创buildiCal文件

我正在寻找一种在c#(asp.net)中生成iCalendar文件(* .ics)的好方法。 我find了一些资源,但是有一点是缺乏的,那就是他们支持quoted-printable的字段 – 有回车和换行的字段。

例如,如果描述字段编码不正确,则只显示第一行,并可能破坏* .ics文件中的其余信息。

我正在寻找可以生成* .ics文件的现有类和/或可以生成带引号的可打印字段的类。

我使用DDay.Ical ,它的好东西。 有能力打开一个ical文件,并获得一个很好的对象模型的数据。 它说testing版,但它对我们很好。

编辑2016年11月

这个库已经被弃用了,但被另外一个开发者拿来作为iCal.NET重新发布。

关于该版本的说明: rianjs.net/2016/07/dday-ical-is-now-ical-net

来源GitHub: github.com/rianjs/ical.net

我发现这样做的最简单的方法是使用微格式标记您的HTML。

如果您正在寻找生成iCalendar文件,那么您可以使用hCalendar微格式,然后添加一个链接,如“添加到日历”,指向:

http://feeds.technorati.com/events/ [您的网页的完整url,包括http://]

Technorati页面然后parsing您的页面,提取hCalendar信息并将iCalendar文件发送到客户端。

我写了一个填充函数来处理这个问题。 这主要是兼容的 – 唯一的挂断是,第一行是74个字符,而不是75(74是处理后续行的空间)…

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 

我用它来概述和描述我们的ICS饲料。 只要提供已经预置的字段的行(例如LongText =“SUMMARY:Event Title”)。 只要你把caching设置得长得多,操作并不算太贵。

iCal (ical 2.0)和quoted-printable不会在一起。

Quoted- PrintablevCal (vCal 1.0)中用于表示不可打印的字符,例如换行符(= 0D = 0A)。 默认的vCal编码是7位,因此有时需要使用quoted-printable来表示非ASCII字符(可以覆盖默认编码,但不需要其他vCal兼容通信方来理解)。

iCal中 ,特殊字符使用转义符表示,例如'\ n'。 默认的编码是UTF-8,所有符合iCal标准的团体都必须支持它,并且在iCal 2.0(和vCard 3.0)中完全不需要quoted-printable。

您可能需要支持您的客户/利益相关方,以澄清要求。 似乎vCal和iCal之间有混淆。

看看http://www.codeproject.com/KB/vb/vcalendar.aspx

它不处理引用的可打印字段像你问,但其余的代码是在那里,可以修改。

根据RFC-2445,注释和说明字段是TEXT。 testing字段的规则是:[1] TEXT字段中的单行不超过75个八位字节。 [2]通过插入一个CRLF,然后是空白来实现包装。 [3]有几个字符必须被编码,包括\(反斜线); (分号),(逗号)和换行符。 使用\(反斜线)作为分隔符给出\ \; \,\ n

示例:以下是属性值中带有格式化分行符的属性的示例:

  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. 

iCal可能会很复杂,所以我build议使用一个库。 DDay是一个很好的免费解决scheme。 最后我检查了它没有完全支持周期性事件,但除此之外,它看起来非常好。 绝对testing几个客户端的日历。

我知道这已经太晚了,但可能会帮助别人。 在我的情况下,我用.ics扩展名写下面的文本文件

 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 

它为我工作。

我错过了一个自定义时区的例子。 所以这里有一个片段,展示了如何在ics中设置时区(并将其发送到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();