簡體   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