通过C#创建VCS文件,但是outlook或C#都不喜欢我的日期(Creating VCS file via C# but either outlook or C# don't like m

编程入门 行业动态 更新时间:2024-10-21 13:29:16
通过C#创建VCS文件,但是outlook或C#都不喜欢我的日期(Creating VCS file via C# but either outlook or C# don't like my dates)

我正在尝试在C#中创建.vcs文件。 基本上在outlook中,如果你添加一个日历约会,它会在outlook中创建一个文件,如下所示:

您实际上可以导出此文件,右键单击它并在您喜欢的文本编辑器中打开它。 它看起来像这样:

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

所以我遇到的问题是DTSTART和DTEND的实际时间。 您可以看到,当我打开outlook文件时,它表示上午11:00(如屏幕截图所示),但在文本文件中我将其作为中午12:00。

所以我有一个应用程序(培训应用程序),我动态创建其中一个vcs文件。 使用C#我收集主题,位置,描述和日期(有时间),如下所示:

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)) { //good date } if (DateTime.TryParseExact(end, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out trainingDateEnd)) { //good date } OpenVCSFile("vcsFile.aspx?TrainingDateBegin=" + trainingDateBegin + "&TrainingDateEnd=" + trainingDateEnd + "&Location=" + location + "&Subject=" + subject + "&Description=" + description, "Utilities", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=500,left=10,top=20"); }

所以在上面的代码中,fromTime就变成了例如早上7点,toTime变成了早上8点。 然后我使用DateTime.TryParseExact将日期与时间合并,因此它变为例如06/01/2012 7:00 am为beginDate和endDate它变为例如06/01/2012 8:00 am 。

到目前为止那么好......然后我只是调用一个函数OpenVCSFile,这只是一些javascript来打开传入的url,如下所示:

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>"); }

然后调用vcsFile.aspx页面,我可以在其中填写outlook值...

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

一切似乎都有效除了最重要的部分,我这样做的部分:

writer.WriteLine("DTSTART:" + beginDate); //why dont the times come out right... writer.WriteLine("DTEND:" + endDate); //same here

正如您在outlook屏幕截图中看到的那样,日期正确,但时间总是错误的...通常情况会在上午10:00到上午11:00之间打开。 但它永远不会花时间给我。 例如,在我的c#代码中,这里是监视屏幕:

trainingDateBegin {12/6/2012 12:00:00 PM} trainingDateEnd {12/6/2012 1:00:00 PM}

所以我的应用程序在12/6/2012的日期过去了,时间是12:00:00 pm到12/6/2012 1:00:00 pm。 但是当这里生成vcs文件的结果是:

(如果图像没有显示,基本上前景是拥有所有正确的信息:主题,位置,开始日期结束日期但是时间错误。它说的是上午11点到下午12点。它几乎就像它使用我的系统时钟EST)...

有谁知道我可能做错了什么。 对不起,长篇帖子:(。

I'm 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:

You can actually export this file, right click on it and open it up 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 what I am having a problem with is the actual time in DTSTART and DTEND from above. You can see that when I open the outlook file it says 11:00am (as the screen shot shows) but in the text file I have it as 12:00pm.

So I have an application (training application) where I dynamically create one of these vcs files. Using C# I gather the subject, location, description and dates (with times) like so:

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)) { //good date } if (DateTime.TryParseExact(end, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out trainingDateEnd)) { //good date } OpenVCSFile("vcsFile.aspx?TrainingDateBegin=" + trainingDateBegin + "&TrainingDateEnd=" + trainingDateEnd + "&Location=" + location + "&Subject=" + subject + "&Description=" + description, "Utilities", "toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=500,left=10,top=20"); }

So in the above code fromTime just becomes for instance 7:00 am and toTime becomes something like 8:00 am. I then use DateTime.TryParseExact to merge the date with the time so it becomes for instance 06/01/2012 7:00 am for beginDate and for endDate it becomes for instance 06/01/2012 8:00 am.

So far so good...then I just call a function OpenVCSFile which is just some javascript to open the passed in url like so:

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>"); }

This then calls the vcsFile.aspx page where I can fill in the outlook 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 appears to work EXCEPT 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 comes out right as you see in the outlook screen shot, but the time is always wrong... Usually outlook will open it up with 10:00 am till 11:00 am. But it never takes the time that I give it. For instance, in my c# code here is the watch screen:

trainingDateBegin {12/6/2012 12:00:00 PM} trainingDateEnd {12/6/2012 1:00:00 PM}

So my app is passing in the date of 12/6/2012 with a time of 12:00:00 pm till 12/6/2012 1:00:00 pm. But then when the vcs file is generated here is the result:

(if the image doesnt show up basically outlook is having all the correct information: subject, location, start date end date BUT the time is wrong. It says 11am till 12pm. Its almost like its using my system clock EST)...

Does anyone know what I might be doing wrong. Sorry for the long post:(.

最满意答案

我认为这与时区有关; 您可以添加时区或(更简单)在您的文件中使用UTC时间。

如果您确实添加了时区,则需要使用vCal版本2,因为Outlook不支持版本1中的时区。


编辑:这是从Wikipedia上关于VCalendar的文章中获取的语法示例。 此事件发生在1997年7月14日17:00(UTC)到1997年7月15日03:59:59(UTC):

BEGIN:VCALENDAR VERSION:2.0 PRODID:-//hacksw/handcal//NONSGML v1.0//EN BEGIN:VEVENT UID:uid1@example.com DTSTAMP:19970714T170000Z ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com DTSTART:19970714T170000Z DTEND:19970715T035959Z SUMMARY:Bastille Day Party END:VEVENT END:VCALENDAR

I think it's to do with timezones; you could either add a timezone or (much simpler) use the UTC time in your file.

If you do add a timezone you'll need to use vCal version 2, since Outlook doesn't support timezones in version 1.


EDIT: This is an example of the syntax taken from the Wikipedia article on VCalendar. This event occurs July 14, 1997 17:00 (UTC) through July 15, 1997 03:59:59 (UTC):

BEGIN:VCALENDAR VERSION:2.0 PRODID:-//hacksw/handcal//NONSGML v1.0//EN BEGIN:VEVENT UID:uid1@example.com DTSTAMP:19970714T170000Z ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com DTSTART:19970714T170000Z DTEND:19970715T035959Z SUMMARY:Bastille Day Party END:VEVENT END:VCALENDAR

更多推荐

本文发布于:2023-07-24 00:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:都不   日期   文件   喜欢   outlook

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!