Outlook互操作打开太多项目导致错误
本文关键字:错误 项目 太多 互操作 Outlook | 更新日期: 2023-09-27 18:18:53
我的目标是打开共享日历进行分析和数据库跟踪。
关键是我设法打开了项目,但在数百之后,我得到了一个异常。它解释了管理员(安全原因)限制了同时打开的项目的数量。
我可以理解这一点,所以我已经尝试关闭每个项目后阅读它。但还是会出现错误。当我读取用户属性时,问题出现了。
下面是我的程序示例:
Recipient recipient = mapiNamespace.CreateRecipient("John Doe");
if (recipient.Resolve())
{
CalendarFolder = mapiNamespace.GetSharedDefaultFolder(recipient, OlDefaultFolders.olFolderCalendar);
outlookCalendarItems = CalendarFolder.Items;
outlookCalendarItems.IncludeRecurrences = false;
}
else
{
Console.Write("Failed to open Calendar");
return;
}
foreach (Microsoft.Office.Interop.Outlook.AppointmentItem item in outlookCalendarItems)
{
Console.WriteLine(item.Subject + " -> " + item.Start.ToLongDateString()); //=> Ok, no problem
UserProperty up = item.UserProperties.Find("Test"); //=> Problem if too many items
if( up!= null )
{
Console.Write("UserProperty Value: " + up.Value);
}
((Microsoft.Office.Interop.Outlook._AppointmentItem)item).Close(OlInspectorClose.olDiscard); //=> Problem if too many items
Console.WriteLine();
}
Console.ReadKey();
你知道如何正确关闭项目吗?
除了使用Masrhal.ReleaseCOMObject
,你还应该避免使用多个点表示法(例如item.UserProperties.Find
)——编译器会为每个"; "创建一个隐式变量。-你不能显式释放这样的变量。
第三,循环遍历文件夹中的所有项目总是一个糟糕的主意-使用MAPIFolder.GetTable
。或者在Redemption中使用MAPITable
对象(我是它的作者):http://www.dimastr.com/redemption/mapitable.htm
我是这样做的(以防有人遇到这个线程):
System.Runtime.InteropServices.Marshal.ReleaseComObject(_calendar_item);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();