The most important part of this task is getting the format for the date and time correct. There is a specific format needed for an ICS file, if the format is not correct then current date time is the default that is used.
Assuming your date is in a string format, you would need to change it the required format using the below code
DateTime.Parse(startDate).ToUniversalTime().ToString(“yyyyMMddT090000”)
Kindly note I have added 090000 as the hour/minute/second as in my case the meeting would be default start at 9 AM.
string filePath = string.Empty;
string path = HttpContext.Current.Server.MapPath(@”\iCal\”);
filePath = path + subject + “.ics”;
StreamWriter writer = new StreamWriter(filePath);
writer.WriteLine(“BEGIN:VCALENDAR”);
writer.WriteLine(“VERSION:2.0”);
writer.WriteLine(“PRODID:-//hacksw/handcal//NONSGML v1.0//EN”);
writer.WriteLine(“BEGIN:VEVENT”);
writer.WriteLine(“DTSTART:” + startDate);
writer.WriteLine(“DTEND:” + endDate );
writer.WriteLine(“SUMMARY:” + subject);
writer.WriteLine(“LOCATION:” + location);
writer.WriteLine(“END:VEVENT”);
writer.WriteLine(“END:VCALENDAR”);
writer.Close();
return filePath;
string fileName = subject + “.ics”;
Response.BufferOutput = false;
Response.AddHeader(“content-disposition”, “attachment; filename=” + fileName);
Response.ContentType = “text/calendar”;
Response.TransmitFile(Server.MapPath(“ICal/” + fileName));
Response.Flush();
Response.End();