Microsoft.Office.Interop.Outlook错误 - 文件filename.ics不是有效的Internet日历文件

时间:2012-11-08 23:05:16

标签: c# outlook interop visual-studio-2012

我正在尝试在Web应用上使用Outlook 2010日历的内容。为此,我在IIS 7.5 / Windows 2008 R2计算机上设置了启用WebDAV的网站。从Outlook 2010开始,我将日历发布到我设置的网站,这导致网站文件夹中出现了一个新的.ics文件。

现在,为了读取/解析文件的内容,我将该文件复制到我的VS 2012计算机上,该计算机与安装Outlook的计算机相同。我使用此示例来解析文件:http://msdn.microsoft.com/en-us/library/office/bb644609%28v=office.12%29.aspx

当我在示例中使用此代码(稍微修改一下)时,我得到2个异常,一个在OpenSharedItem上,表示Operation Failed,另一个在OpenSharedFolder上,后者表示该文件不是有效的Internet日历文件。 / p>

string fileName = "filename.ics";

if (!File.Exists(fileName))
    Console.WriteLine("File Does NOT Exist");
else
    Console.WriteLine("File Exists");

// First try to open the icalendar file as an appointment 
// (not a calendar folder).
object item = null;
try
{
    Application a = new Application();

    item = a.Session.OpenSharedItem(fileName);
    Console.WriteLine("open shared item");
}
catch(System.Exception e)//gets thrown
{
    Console.WriteLine("Open shared item failed");
    Console.WriteLine(e.Message + e.StackTrace);
}

if (item != null)
{
    Console.WriteLine("item not null");

    // Display the item
    OutlookItem olItem = new OutlookItem(item);
    olItem.Display();
    return;
}

// If unsucessful in opening it as an item, 
// try opening it as a folder
Outlook.Folder importedFolder = null;
try
{
    Application a = new Application();              
    importedFolder = a.Session.OpenSharedFolder(fileName, Type.Missing, Type.Missing, Type.Missing) as Outlook.Folder;
    Console.WriteLine("open shared folder");
}
catch (System.Exception e)//gets thrown
{
    Console.WriteLine("Open shared folder failed");
    Console.WriteLine(e.Message + e.StackTrace);
}

// If sucessful, open the folder in a new explorer window
if (importedFolder != null)
{
    //never reached
}

filename.ics

BEGIN:VCALENDAR
PRODID:-//Microsoft Corporation//Outlook 14.0 MIMEDIR//EN
VERSION:2.0
METHOD:PUBLISH
X-CALSTART:20121009T150000Z
X-CALEND:20130102T213000Z
X-CLIPSTART:20121006T050000Z
X-CLIPEND:20130105T060000Z
X-WR-RELCALID:{0000002E-01FB-5DE5-BFA0-7A2B5F98AC2C}
X-WR-CALNAME:Doe\, John
X-WR-CALDESC:
X-PRIMARY-CALENDAR:TRUE
X-PUBLISHED-TTL:PT60M
X-OWNER;CN="Doe, John":mailto:jdoe@mycompany.com
X-MS-OLK-WKHRSTART;TZID="Central Standard Time":080000
X-MS-OLK-WKHREND;TZID="Central Standard Time":170000
X-MS-OLK-WKHRDAYS:MO,TU,WE,TH,FR
BEGIN:VTIMEZONE
TZID:Central Standard Time
BEGIN:STANDARD
DTSTART:16011104T020000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11
TZOFFSETFROM:-0500
TZOFFSETTO:-0600
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010311T020000
RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3
TZOFFSETFROM:-0600
TZOFFSETTO:-0500
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
DTEND:20121009T160000Z
DTSTAMP:20121105T230030Z
DTSTART:20121009T150000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:FVI3qgvnnECOutT6Cpus2A==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121009T200000Z
DTSTAMP:20121105T230030Z
DTSTART:20121009T190000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:1pvycFq1HUqwrcPUvvSd9Q==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121010T203000Z
DTSTAMP:20121105T230030Z
DTSTART:20121010T190000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:GtLjxE2g1EqBQdtFDOUe7w==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121011T160000Z
DTSTAMP:20121105T230030Z
DTSTART:20121011T150000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:W4b2WmjZ6Uu9SEKaObx6dw==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121011T210000Z
DTSTAMP:20121105T230030Z
DTSTART:20121011T193000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:nU45vLjlkU23vbmmS5ANPw==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121015T200000Z
DTSTAMP:20121105T230030Z
DTSTART:20121015T190000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Tentative
TRANSP:OPAQUE
UID:qbw5o6Y1HUugYh2RWxeDHw==
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
END:VEVENT
BEGIN:VEVENT
DTEND:20121016T160000Z
DTSTAMP:20121105T230030Z
DTSTART:20121016T150000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Tentative
TRANSP:OPAQUE
UID:V10iWliy10yKQMl/UaLQlQ==
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
END:VEVENT
BEGIN:VEVENT
DTEND:20121017T203000Z
DTSTAMP:20121105T230030Z
DTSTART:20121017T190000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:U3ycfGWWwkOt12msYYEHLw==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121018T160000Z
DTSTAMP:20121105T230030Z
DTSTART:20121018T150000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:U8psiL5Roke/iwHrSSbr8g==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121018T210000Z
DTSTAMP:20121105T230030Z
DTSTART:20121018T193000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:mjasBcST7k+jDlXJw/1MQA==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121022T200000Z
DTSTAMP:20121105T230030Z
DTSTART:20121022T190000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Tentative
TRANSP:OPAQUE
UID:vsYg7aai30GmYQZciLrkuA==
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
END:VEVENT
BEGIN:VEVENT
DTEND:20121023T160000Z
DTSTAMP:20121105T230030Z
DTSTART:20121023T150000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Tentative
TRANSP:OPAQUE
UID:y+3dporUvkq8uqjnnBGm3Q==
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
END:VEVENT
BEGIN:VEVENT
DTEND:20121023T200000Z
DTSTAMP:20121105T230030Z
DTSTART:20121023T190000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:gjP1oxozi06EFNPsIvcb7A==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121024T203000Z
DTSTAMP:20121105T230030Z
DTSTART:20121024T190000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:OD5UhYsBB0+e/lFxl5ubzg==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121025T160000Z
DTSTAMP:20121105T230030Z
DTSTART:20121025T150000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:iuN26STSskaIFUQeSPjfBw==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121025T210000Z
DTSTAMP:20121105T230030Z
DTSTART:20121025T193000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:jnNtwU6AWkGGBpG6wOZMmA==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121026T150000Z
DTSTAMP:20121105T230030Z
DTSTART:20121026T140000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Tentative
TRANSP:OPAQUE
UID:SVZHk/EV0Eev8zr1p3bVrg==
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
END:VEVENT
BEGIN:VEVENT
DTEND:20121029T200000Z
DTSTAMP:20121105T230030Z
DTSTART:20121029T180000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Tentative
TRANSP:OPAQUE
UID:4+5yxy98ak+fgwy6zrbNRg==
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
END:VEVENT
BEGIN:VEVENT
DTEND:20121030T160000Z
DTSTAMP:20121105T230030Z
DTSTART:20121030T150000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Tentative
TRANSP:OPAQUE
UID:jub5aDFivEGbqhBJwc3Vpg==
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
END:VEVENT
BEGIN:VEVENT
DTEND:20121030T200000Z
DTSTAMP:20121105T230030Z
DTSTART:20121030T190000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:9il1KMwbekKQA4kfjhvljg==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121031T210000Z
DTSTAMP:20121105T230030Z
DTSTART:20121031T190000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:PAmC9pXGFEeP8uEVZ2e8GQ==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121101T160000Z
DTSTAMP:20121105T230030Z
DTSTART:20121101T150000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:mzHkS2g/4kiLE9EF1qHP3Q==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121101T210000Z
DTSTAMP:20121105T230030Z
DTSTART:20121101T193000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:UGc1w8m2iEW+BHUxCyd9xg==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121106T170000Z
DTSTAMP:20121105T230030Z
DTSTART:20121106T160000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Tentative
TRANSP:OPAQUE
UID:wi/8np16uESdCzI471NFZA==
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
END:VEVENT
BEGIN:VEVENT
DTEND:20121107T213000Z
DTSTAMP:20121105T230030Z
DTSTART:20121107T200000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:Mnf3wC1+CE6qW98clLFycQ==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121108T170000Z
DTSTAMP:20121105T230030Z
DTSTART:20121108T160000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:1fEZ3YnxEk2P4BTzw8dcNw==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121108T220000Z
DTSTAMP:20121105T230030Z
DTSTART:20121108T203000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:9KlqqMOAPEi4qFSOhTxImQ==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121113T170000Z
DTSTAMP:20121105T230030Z
DTSTART:20121113T160000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Tentative
TRANSP:OPAQUE
UID:VB9LJPflDkm24bxG0TBX/Q==
X-MICROSOFT-CDO-BUSYSTATUS:TENTATIVE
END:VEVENT
BEGIN:VEVENT
DTEND:20121114T213000Z
DTSTAMP:20121105T230030Z
DTSTART:20121114T200000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:3bIwxwBzEEiNFhFpaQ72tA==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121115T170000Z
DTSTAMP:20121105T230030Z
DTSTART:20121115T160000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:Zm2nzgnCE0yio988QdmWOg==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121121T213000Z
DTSTAMP:20121105T230030Z
DTSTART:20121121T200000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:kX2JXLt0YkW/9jEvIM/uGw==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121128T213000Z
DTSTAMP:20121105T230030Z
DTSTART:20121128T200000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:8Z2xP1+j2UyvsXtqk9/x3w==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121129T220000Z
DTSTAMP:20121105T230030Z
DTSTART:20121129T203000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:VaD+s7gJC0qi/m/ZaglYVA==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121205T213000Z
DTSTAMP:20121105T230030Z
DTSTART:20121205T200000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:bq09F62hpkGFaaJnci9rwQ==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121206T220000Z
DTSTAMP:20121105T230030Z
DTSTART:20121206T203000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:gdb/P27fOkOgdnPlsdgQ6g==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121212T213000Z
DTSTAMP:20121105T230030Z
DTSTART:20121212T200000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:zcV/ETzsK0CJ3Luyc/1pZQ==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121213T220000Z
DTSTAMP:20121105T230030Z
DTSTART:20121213T203000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:bSdV5Ln7Ok+GHu1bIrm9yg==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121219T213000Z
DTSTAMP:20121105T230030Z
DTSTART:20121219T200000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:7rNe2ZxZp0WvkPf5C0IsNA==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20121226T213000Z
DTSTAMP:20121105T230030Z
DTSTART:20121226T200000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:2sgn16IkbUycYLaDZFD4Ug==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
BEGIN:VEVENT
DTEND:20130102T213000Z
DTSTAMP:20121105T230030Z
DTSTART:20130102T200000Z
SEQUENCE:0
SUMMARY;LANGUAGE=en-us:Busy
TRANSP:OPAQUE
UID:91urQDvoIkqcLS8xpDscXg==
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
END:VEVENT
END:VCALENDAR

2 个答案:

答案 0 :(得分:1)

您可以尝试文件的完整路径吗?

示例:

string fileName = "c:\temp\filename.ics";

编辑:

阅读OpenSharedItem我发现:

  

此方法不支持 iCalendar日历(.ics)文件。打开   iCalendar日历文件,可以使用OpenSharedFolder方法   NameSpace对象。

但在opensharedfolder我发现:

  

此方法不支持 iCalendar约会(.ics)文件。至   打开iCalendar约会文件,即可使用OpenSharedItem   NameSpace对象的方法。

所以,我们的第一个问题是:这是iCalendar日历还是iCalendar约会?回答这个问题我们知道是否需要使用OpenSharedItem或OpenSharedFolder。

答案 1 :(得分:1)

OpenSharedItem仅适用于具有一个VEVENT的ICS文件。如果ICS文件包含一个VEVENT而其他VEVENTS是第一个VEVENT的例外,则将标题X-MS-OLK-FORCEINSPECTOROPEN:TRUE添加到ICS文件中,例如:

BEGIN:VCALENDAR
VERSION:2.0
X-MS-OLK-FORCEINSPECTOROPEN:TRUE

现在,OpenSharedItem将毫无问题地加载ics文件。