Introduction
Very simple to create vCalendar files, it is basically like creating text files, the only difference is the format.
In this example, a physical file is not created (no need to clean up a temp directory afterwards), the file is kept in memory using memory stream. Once the file is created in memory, it is sent for download.
vCalendar is using UTC, so the function ToUniversalTime
is used to convert the local time to UTC.
<%@ Page Language="vb" ContentType="text/html"
ResponseEncoding="iso-8859-1" Debug="False" trace="False"%>
<%@ import Namespace="System.IO" %>
<script runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim beginDate as Date = #01/07/2005 4:00 PM#
Dim endDate as Date = #01/07/2005 6:00 PM#
Dim myLocation as String = "Computer Room"
Dim mySubject as String = "Training"
Dim myDescription as String = "Event details"
Dim mStream As new MemoryStream()
Dim writer As new StreamWriter(mStream)
writer.AutoFlush = true
writer.WriteLine("BEGIN:VCALENDAR")
writer.WriteLine("PRODID:-//Flo Inc.//FloSoft//EN")
writer.WriteLine("BEGIN:VEVENT")
writer.WriteLine("DTSTART:" & _
beginDate.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z") )
writer.WriteLine("DTEND:" & _
endDate.ToUniversalTime.ToString("yyyyMMdd\THHmmss\Z") )
writer.WriteLine("LOCATION:" & myLocation)
writer.WriteLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" & myDescription)
writer.WriteLine("SUMMARY:" & mySubject)
writer.WriteLine("PRIORITY:3")
writer.WriteLine("END:VEVENT")
writer.WriteLine("END:VCALENDAR")
Response.Clear()
Response.AppendHeader("Content-Disposition", _
"attachment; filename=Add2Calendar.vcs")
Response.AppendHeader("Content-Length", mStream.Length.ToString())
Response.ContentType = "application/download"
Response.BinaryWrite(mStream.ToArray())
Response.End()
End Sub
</script>