繁体   English   中英

C# Interop Outlook 联系人不遍历所有项目

[英]C# Interop Outlook Contacts not iterating through all Items

我想删除所有带有 CustomerID 的 Outlook 联系人。 所以我认为使用 Interop 获取所有联系人并遍历它们并检查它们是否具有我想要的 CustomerID 会很容易。

所以这是我编码的:

var app = new Application();

var folderContacts = app
  .ActiveExplorer()
  .Session
  .GetDefaultFolder(OlDefaultFolders.olFolderContacts);

var searchFolder = folderContacts.Items;

foreach (ContactItem foundContact in searchFolder)
    if (foundContact.CustomerID == myCustomerIdAsString)
        foundContact.Delete();

这将获取所有联系人,但我的问题是,它不会遍历所有项目。 看这张图片:

执行后我的代码

你可以看到,它遍历了大约一半的项目。 但我不知道为什么。

有人知道该怎么做吗?

就像 Yosh 在他的评论中所写的那样, searchFolder Items 在迭代本身中发生了变异。 因此,我将要删除的实体放在一个集合中,并在迭代后删除它们。 我想删除所有带有 CustomerID 的 Outlook 联系人。 所以我认为使用 Interop 获取所有联系人并遍历它们并检查它们是否具有我想要的 CustomerID 会很容易。

所以这是我编码的:

var app = new Application();

var contacts = new List<ContactItem>();

var folderContacts = app
  .ActiveExplorer()
  .Session
  .GetDefaultFolder(OlDefaultFolders.olFolderContacts);

var searchFolder = folderContacts.Items;

foreach (ContactItem foundContact in searchFolder)
    if (foundContact.CustomerID == myCustomerIdAsString)
        contacts.Add(foundContact);

contacts.ForEach(x => x.Delete());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM