简体   繁体   English

如何使用 c# 创建 .ics 文件?

[英]How to create .ics file using c#?

在此处输入图片说明 I used below code for creating .ics file but it's not working can any one help me,where its going wrong.我使用下面的代码来创建 .ics 文件,但它不起作用任何人都可以帮助我,哪里出错了。

System.Text.StringBuilder sbICSFile = new System.Text.StringBuilder();
sbICSFile.AppendLine("BEGIN:VCALENDAR");
sbICSFile.AppendLine("VERSION:2.0");
sbICSFile.AppendLine("PRODID:-//ICSTestCS/");
sbICSFile.AppendLine("CALSCALE:GREGORIAN");
sbICSFile.AppendLine("BEGIN:VTIMEZONE");

----------
----------
Response.ContentType = "text/calendar";
Response.ContentEncoding = Encoding.UTF8;
Response.Charset = "utf-8";
Response.AddHeader("Content-Disposition", "attachment;filename=Test.ics");
Response.Write(sbICSFile);
Response.End();

This is how I usually create .ics files.这就是我通常创建.ics文件的方式。

//some variables for demo purposes
DateTime DateStart = DateTime.Now;
DateTime DateEnd = DateStart.AddMinutes(105);
string Summary = "Small summary text";
string Location = "Event location";
string Description = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.";
string FileName = "CalendarItem";

//create a new stringbuilder instance
StringBuilder sb = new StringBuilder();

//start 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 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 time zone specified
sb.AppendLine("DTSTART;TZID=Europe/Amsterdam:" + DateStart.ToString("yyyyMMddTHHmm00"));
sb.AppendLine("DTEND;TZID=Europe/Amsterdam:" + DateEnd.ToString("yyyyMMddTHHmm00"));
//or without
sb.AppendLine("DTSTART:" + DateStart.ToString("yyyyMMddTHHmm00"));
sb.AppendLine("DTEND:" + DateEnd.ToString("yyyyMMddTHHmm00"));

sb.AppendLine("SUMMARY:" + Summary + "");
sb.AppendLine("LOCATION:" + Location + "");
sb.AppendLine("DESCRIPTION:" + Description + "");
sb.AppendLine("PRIORITY:3");
sb.AppendLine("END:VEVENT");

//end calendar item
sb.AppendLine("END:VCALENDAR");

//create a string from the stringbuilder
string CalendarItem = sb.ToString();

//send the calendar item to the browser
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "text/calendar";
Response.AddHeader("content-length", CalendarItem.Length.ToString());
Response.AddHeader("content-disposition", "attachment; filename=\"" + FileName + ".ics\"");
Response.Write(CalendarItem);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();

In addition to the accepted answer, you must also add UID and DTSTAMP to work for google calendar.除了接受的答案外,您还必须添加 UID 和 DTSTAMP 才能用于谷歌日历。 Here's what I added:这是我添加的内容:

sb.AppendLine("UID:" + (Guid.NewGuid().ToString() + DateTime.Now.ToString("yyyyMMddTHHmm00")));
sb.AppendLine("DTSTAMP:" + DateTime.Now.ToString("yyyyMMddTHHmm00"));

You can also test your content here .您还可以在此处测试您的内容。

I thrashed on building ics files that supported timezones.我努力构建支持时区的 ics 文件。 I ended up going with ical.Net to produce the files.我最终使用ical.Net来生成文件。 Their wiki has a basic example of how to produce the file.他们的wiki有一个关于如何生成文件的基本示例。 Here is what it looks like when you need timezone support.这是您需要时区支持时的样子。

var startDate = DateTime.Now;
var endDate = startDate.AddHours(1);
var calendar = new Calendar
               {
                  Method = "PUBLISH",
               };
calendar.AddTimeZone("America/Chicago");
var calendarEvent = new CalendarEvent
        {
            Description = "Your description here",
            End = new CalDateTime(endDate, "America/Chicago"),
            Location = "Your location here",
            Start = new CalDateTime(startDate, "America/Chicago"),
            Summary = "Your summary here"
        };
calendar.Events.Add(calendarEvent);

Then to get the ics file as a string:然后以字符串形式获取 ics 文件:

var serializer = new CalendarSerializer();
var iscFile = serializer.SerializeToString(calendar);

Finally I'd recommend validating your results with this tool .最后,我建议使用此工具验证您的结果。 Hope that saves someone some time.希望能节省一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM