我有以下代码,从通讯组列表,交换服务器中检索联系人项目需要花费大量时间。有什么可以调整它的性能的
吗public Outlook.AddressEntry GetDistributionListMembers(string sParamName,Outlook.Application _OutlookApp) { try { List<string> allAddressEntriesList = new List<string>(); Outlook.AddressLists addrLists = _OutlookApp.ActiveExplorer().Session.AddressLists; var receipientContactList = new List<Outlook.AddressEntry>(); foreach (Outlook.AddressList addrList in addrLists) { if (addrList.AddressEntries.Count <= 0) continue; receipientContactList.AddRange(addrList.AddressEntries.Cast<Outlook.AddressEntry>() .Where(x => x.Address.ToLower().Equals(sParamName.ToLower())));
如果调试
Debug.print(addrList.Name);
恩迪夫
if (receipientContactList.Count > 0) break; } return receipientContactList.FirstOrDefault(x => x.GetContact().HasPicture) ?? receipientContactList.FirstOrDefault(); } catch (System.Exception ex) { WriteLog(System.Reflection.MethodBase.GetCurrentMethod().Name,
前任。消息(; 返回空值; }
}
首先,永远不要遍历通讯簿中的所有条目。您正在匹配地址;为什么不使用Namespace.CreateRecipient
/Recipient.Resolve
?
其次,您假设所有地址条目都来自"联系人"文件夹。在 Exchange 下使用 GAL 的情况下,情况并非如此,AddressEntry.GetContact()
将返回 null,从而导致异常。
如果您的条目始终来自"联系人"文件夹,为什么不使用Namespace.GetDefaultFolder(olFolderContacts)
然后使用MAPIFolder.Items.Find
?