C# 展望例外

C# 展望例外,c#,com,outlook,outlook-2010,comexception,C#,Com,Outlook,Outlook 2010,Comexception,System.Runtime.InteropServices.ComeException..服务器管理员限制了您可以同时打开的项目数 在Microsoft.Office.Interop.Outlook.\u AppointmentItem.get\u UserProperties()中 var calendar=outlookApplication.GetNamespace(“MAPI”).GetDefaultFolder(OlDefaultFolders.olFolderCalendar);

System.Runtime.InteropServices.ComeException..服务器管理员限制了您可以同时打开的项目数

在Microsoft.Office.Interop.Outlook.\u AppointmentItem.get\u UserProperties()中

var calendar=outlookApplication.GetNamespace(“MAPI”).GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
如果(日历==null | |日历项目==null)
{
返回null;
}
var calendarItems=calendar.Items;
if(calendarItems!=null&&calendarItems.Count>0)
{
//请不要转换为LINQ或foreach->可能会导致Outlook内存泄漏。

对于(int counter=1;counter我已经找到了这个问题的解决方案。没有必要使用
Find
cause
Restrict
来满足我的需要

...
string filter = string.Format("[myInformation] = '{0}'", myInformation);
var calendarItems = calendar.Items.Restrict(filter);
...

您需要确保使用
Marshal.ReleaseComObject()
处理完Outlook项目后立即释放它们,而不是等待垃圾收集器启动


您还需要避免使用多点表示法,以确保不会获得包含中间结果且无法显式引用和发布的隐式变量(由编译器创建)。

处理Outlook邮件项目时,请关闭它,然后发布它

For Each item As Outlook.MailItem In oFolder.Items
  '<process item code>

  'Close the item
    'SaveMode Values
    'olDiscard  = 1
    'olPromptForSave = 2
    'olSave = 0
  item.Close(1)

  'Release item
  Marshal.ReleaseComObject(item)
Next
在oFolder.Items中为每个项目设置Outlook.MailItem

'
'关闭项目
'保存模式值
'olDiscard=1
'olPromptForSave=2
'olSave=0
项目.结束(1)
'发布项目
Marshal.ReleaseComObject(项目)
下一个

thx(用于投票)这意味着有人读了它,发现它很好)你找到了解决方案吗?@Anubis1233-我已经发布了我的答案谢谢,但我已经在使用限制:/@Anubis1233你对使用限制有异议吗?你有没有为你的问题创建问题?太好了,谢谢-我也有同样的问题,总是关闭对象,但释放ComObject是错误的解决方案:)
For Each item As Outlook.MailItem In oFolder.Items
  '<process item code>

  'Close the item
    'SaveMode Values
    'olDiscard  = 1
    'olPromptForSave = 2
    'olSave = 0
  item.Close(1)

  'Release item
  Marshal.ReleaseComObject(item)
Next