I am trying to create a .vcs file in C #. Basically in Outlook, if you add a calendar appointment, it creates a file that in Outlook looks like this:
In fact, you can export this file, right-click on it and open it in your favorite text editor. It looks something like this:
BEGIN:VCALENDAR PRODID:-//Flo Inc.//FloSoft//EN BEGIN:VEVENT DTSTART:6/12/2012 12:00:00 PM DTEND:6/12/2012 1:00:00 PM LOCATION:Meeting room 1 DESCRIPTION;ENCODING=QUOTED-PRINTABLE:Learn about assets. SUMMARY:asset management training. X-MICROSOFT-CDO-BUSYSTATUS:OOF PRIORITY:5 END:VEVENT END:VCALENDAR
So I have a problem with the actual time in DTSTART and DTEND on top. You can see that when I open the perspective file, it says 11:00 (as shown in the screenshot), but in the text file I have it at 12:00.
So, I have an application (training application) where I dynamically create one of these vcs files. Using C #, I compile the theme, location, description and dates (over time) as follows:
protected void btnOutlook_Click(object sender, EventArgs e) { string location; string description; string subject; string fromTime; string toTime; location = txtLocation.Text; description = txtDescription.Text; subject = lblTitle.Text; fromTime = ddlFromTimeHH.SelectedItem.Text + ":" + ddlFromTimeMM.SelectedItem.Text + ddlFromTimeAMPM.SelectedItem.Text; toTime = ddlToTimeHH.SelectedItem.Text + ":" + ddlToTimeMM.SelectedItem.Text + ddlToTimeAMPM.SelectedItem.Text; string begin = lblDate.Text + " " + fromTime; string end = lblDate.Text + " " + toTime; string format = "dd/MM/yyyy h:mmtt"; DateTime trainingDateBegin; DateTime trainingDateEnd; if (DateTime.TryParseExact(begin, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out trainingDateBegin)) {
So, in the above code, fromTime just appears, like 7:00, for example, and toTime becomes something like 8:00 in the morning. Then I use DateTime.TryParseExact to merge the date with time, so it becomes, for example, 06/01/2012 7:00 am
for beginDate
, and for endDate
it becomes, for example, 06/01/2012 8:00 am
.
So far so good ... then I just call the OpenVCSFile function, which is just some javascript, to open the passed in url, for example:
protected void OpenVCSFile(string url, string name, string att) { Response.Write("<script language='JavaScript'>"); Response.Write("x=window.open('" + url + "', '" + name + "','" + att + "');"); Response.Write("x.focus();"); Response.Write("</script>"); }
Then the vcsFile.aspx
page is vcsFile.aspx
, where I can fill in the forecast values ...
protected void Page_Load(object sender, EventArgs e) { DateTime beginDate; DateTime endDate; string location; string description; string subject; beginDate = Convert.ToDateTime(Request.QueryString["TrainingDateBegin"]); endDate = Convert.ToDateTime(Request.QueryString["TrainingDateEnd"]); location = Request.QueryString["Location"]; description = Request.QueryString["Description"]; subject = Request.QueryString["Subject"]; MemoryStream mStream = new MemoryStream(); StreamWriter writer = new StreamWriter(mStream); writer.AutoFlush = true; //header writer.WriteLine("BEGIN:VCALENDAR"); writer.WriteLine("PRODID:-//Flo Inc.//FloSoft//EN"); writer.WriteLine("BEGIN:VEVENT"); //BODY writer.WriteLine("DTSTART:" + beginDate); //why dont the times come out right... writer.WriteLine("DTEND:" + endDate); //same here writer.WriteLine("LOCATION:" + location); writer.WriteLine("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:" + description); writer.WriteLine("SUMMARY:" + subject); writer.WriteLine("X-MICROSOFT-CDO-BUSYSTATUS:OOF"); //FOOTER writer.WriteLine("PRIORITY:5"); writer.WriteLine("END:VEVENT"); writer.WriteLine("END:VCALENDAR"); //MAKE IT DOWNLOADABLE Response.Clear(); //clears the current output content from the buffer Response.AppendHeader("Content-Disposition", "attachment; filename=AddToOutlookCalendar.vcs"); Response.AppendHeader("Content-Length", mStream.Length.ToString()); Response.ContentType = "application/download"; Response.BinaryWrite(mStream.ToArray()); Response.End(); }
Everything seems to work. EXCLUDES the most important part, the section where I do this:
writer.WriteLine("DTSTART:" + beginDate); //why dont the times come out right... writer.WriteLine("DTEND:" + endDate); //same here
The date is displayed correctly, as you see in the screenshot, but the time is always wrong ... Usually the appearance will open from 10:00 to 11:00. But I do not have enough time that I give him. For example, in my C # code there is a clock screen:
trainingDateBegin {12/6/2012 12:00:00 PM} trainingDateEnd {12/6/2012 1:00:00 PM}
So, my application runs on the day of 12/6/2012 from 12:00 to 12:00 to 13:00:00. But then when the vcs file is generated, this is the result:
(if the image does not appear, basically Outlook has all the correct information: subject, location, end date, but the time is wrong. It says from 11 am to 12 pm. It almost looks like using my EST system clock) ...
Does anyone know what I can do wrong. Sorry for the long post: (.