在 C# 中创建 iCal 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating iCal Files in c#
提问by Otto
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 for quoted-printablefields - fields that have carriage returns and line feeds.
我正在寻找一种在 c# (asp.net) 中生成 iCalendar 文件 (*.ics) 的好方法。我找到了一些资源,但缺少的一件事是它们对带引号的可打印字段的支持- 具有回车和换行符的字段。
For example, if the descriptionfield isn't encoded properly, only the first line will display and possibly corrupting the rest of the information in the *.ics file.
例如,如果描述字段的编码不正确,则只会显示第一行,并且可能会破坏 *.ics 文件中的其余信息。
I'm looking for existing classes that can generate *.ics files and/or a class that can generate quoted-printablefields.
我正在寻找可以生成 *.ics 文件的现有类和/或可以生成带引号的可打印字段的类。
回答by DevelopingChris
I use DDay.Ical, its good stuff. Has the ability to open up an ical file and get its data in a nice object model. It says beta, but it works great for us.
我使用DDay.Ical,它的好东西。能够打开一个 ical 文件并在一个很好的对象模型中获取其数据。它说是测试版,但它对我们很有用。
Edit Nov 2016
2016 年 11 月编辑
This library has been deprecated, but was picked up and re-released as iCal.NET by another dev.
该库已被弃用,但被另一位开发人员选择并重新发布为 iCal.NET。
Notes about the release: rianjs.net/2016/07/dday-ical-is-now-ical-net
发布说明:rianjs.net/2016/07/dday-ical-is-now-ical-net
Source on GitHub: github.com/rianjs/ical.net
GitHub 上的来源:github.com/rianjs/ical.net
回答by Lance Fisher
iCal can be complicated, so I recommend using a library. DDay is a good free solution. Last I checked it didn't have full support for recurring events, but other than that it looks really nice. Definitely test the calendars with several clients.
iCal 可能很复杂,因此我建议使用库。DDay 是一个很好的免费解决方案。最后我检查了它没有完全支持重复发生的事件,但除此之外它看起来非常好。一定要与多个客户一起测试日历。
回答by slolife
Check out http://www.codeproject.com/KB/vb/vcalendar.aspx
查看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.
它不会像您要求的那样处理带引号的可打印字段,但其余的代码在那里并且可以修改。
回答by Ian Oxley
The easiest way I've found of doing this is to markup your HTML using microformats.
我发现这样做的最简单方法是使用microformats标记您的 HTML 。
If you're looking to generate iCalendar files then you could use the hCalendar microformatthen include a link such as 'Add to Calendar' that points to:
如果您想生成 iCalendar 文件,那么您可以使用hCalendar 微格式,然后包含一个链接,例如“添加到日历”,该链接指向:
http://feeds.technorati.com/events/[your page's full URL including the http:// ]
http://feeds.technorati.com/events/[您页面的完整 URL,包括 http:// ]
The Technorati page then parses your page, extracts the hCalendar info and sends the iCalendar file to the client.
Technorati 页面然后解析您的页面,提取 hCalendar 信息并将 iCalendar 文件发送到客户端。
回答by Ian Oxley
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
根据 RFC-2445,comment 和 description 字段是 TEXT。测试字段的规则是: [1] TEXT 字段中的单行不超过 75 个八位字节。[2] 包装是通过插入一个 CRLF 后跟空格来实现的。[3] 有几个字符必须编码,包括\(反斜杠);(分号)、(逗号)和换行符。使用\(反斜杠)作为分隔符给出\; \, \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.
回答by azheglov
iCal(ical 2.0) and quoted-printable don't go together.
iCal(ical 2.0) 和quoted-printable 不能一起使用。
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.)
Quoted-printable 在vCal(vCal 1.0) 中被大量使用来表示不可打印的字符,例如换行符 (=0D=0A)。默认的 vCal 编码为 7 位,因此有时需要使用带引号的可打印来表示非 ASCII 字符(您可以覆盖默认编码,但不需要其他符合 vCal 的通信方理解它。)
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).
在iCal 中,特殊字符使用转义符表示,例如 '\n'。默认编码是 UTF-8,所有 iCal 兼容方都必须支持它,这使得 iCal 2.0(和 vCard 3.0,就此而言)完全不需要引用打印。
You may need to back your customer/stakeholder to clarify the requirements. There seems to be confusion between vCal and iCal.
您可能需要支持您的客户/利益相关者来澄清要求。vCal 和 iCal 之间似乎存在混淆。
回答by atmarx
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)...
我写了一个 shim 函数来处理这个问题。它主要是合规的——唯一的问题是第一行是 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
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.
我将其用于 ICS 提要的摘要和描述。只需输入已预先添加字段的行(例如 LongText = "SUMMARY:Event Title")。只要您将缓存设置得足够长,操作成本就不会太高。
回答by Manpreet Singh Dhillon
i know it is too late, but it may help others. in my case i wrote following text file with .ics extension
我知道为时已晚,但它可能会帮助其他人。就我而言,我写了以下带有 .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
it worked for me.
它对我有用。
回答by VDWWD
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).
我缺少自定义时区的示例。所以这里有一个片段,展示了如何在 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();