Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#互操作Outlook联系人不遍历所有项目_C#_Interop - Fatal编程技术网

C#互操作Outlook联系人不遍历所有项目

C#互操作Outlook联系人不遍历所有项目,c#,interop,C#,Interop,我想删除所有具有CustomerID的Outlook联系人。因此,我认为使用Interop获取所有联系人并遍历它们并检查它们是否具有我想要的CustomerID是很容易的 这就是我的代码: var app = new Application(); var folderContacts = app .ActiveExplorer() .Session .GetDefaultFolder(OlDefaultFolders.olFolderContacts); var searchFo

我想删除所有具有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();
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());
这会获取所有联系人,但我的问题是,它不会遍历所有项目。请看这张图片:

您可以看到,它迭代了大约一半的项。但我不知道为什么


有人知道该怎么办吗

正如Yosh在评论中所说,searchFolder项在迭代过程中发生了变化。因此,我将要删除的实体放在集合中,并在迭代后删除它们。 我想删除所有具有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();
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());
var-app=新应用程序();
var contacts=新列表();
var folderContacts=app
.ActiveExplorer()
一场
.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
var searchFolder=folderContacts.Items;
foreach(ContactItem在searchFolder中找到Contact)
if(foundContact.CustomerID==mycustomeridassString)
联系人。添加(foundContact);
contacts.ForEach(x=>x.Delete());

我的第一个猜测是,这是因为searchFolder项目在迭代过程中发生了变化(通过删除项目)。您可以使用从末尾开始并递减的“老式”For循环来测试它:
For(int i=searchFolder.Count;i>0;i--)if(searchFolder[i].CustomerID==myCustomerIdAsString)searchFolder.Remove(i)@Yosh我想你是对的。但是我离开了我的foreach并将联系人添加到列表中。在foreach之后,我执行以下操作:
contacts.foreach(x=>x.Delete())并且它工作正常。谢谢