通过C#创建VCS文件,但outlook或C#都不喜欢我的日期

oJM*_*86o 4 c# vcalendar

我正在尝试在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
Run Code Online (Sandbox Code Playgroud)

所以我遇到的问题是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");
        }
Run Code Online (Sandbox Code Playgroud)

所以在上面的代码fromTime中就变成了例如早上7点,toTime变成了早上8点.然后我使用DateTime.TryParseExact将日期与时间合并,因此它变为例如06/01/2012 7:00 amfor beginDate和for 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>");
        }
Run Code Online (Sandbox Code Playgroud)

然后调用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();
        }
Run Code Online (Sandbox Code Playgroud)

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

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

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

trainingDateBegin {12/6/2012 12:00:00 PM}
trainingDateEnd {12/6/2012 1:00:00 PM}
Run Code Online (Sandbox Code Playgroud)

因此,我的应用程序将在2012年12月6日的时间内通过,时间为12:00:00 pm至12/6/2012 1:00:00 pm.但是当这里生成vcs文件时,结果如下:

在此输入图像描述

(如果图像犯规显示基本前景拥有所有正确的信息:主题,地点,开始日期结束日期,但时间是错误的报告说,上午11点至中午12点它几乎就像用我的系统时钟EST的.)...

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

Col*_*ard 7

我认为这与时区有关; 您可以添加时区或(更简单)在您的文件中使用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
Run Code Online (Sandbox Code Playgroud)