如何使用C#创建.ics文件? [英] How to create .ics file using c#?

查看:74
本文介绍了如何使用C#创建.ics文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下代码来创建.ics文件,但是它不能正常工作,任何人都可以帮到我,哪里出错了.

I used below code for creating .ics file but it's not working can any one help me,where its going wrong.

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();

推荐答案

这是我通常创建 .ics 文件的方式.

This is how I usually create .ics files.

//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();

这篇关于如何使用C#创建.ics文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆