2010-09-27 40 views
1
Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application(); 
Microsoft.Office.Interop.Outlook.MAPIFolder calendarFolder = outlookApp.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar); 

Console.WriteLine(calendarFolder.Items.Count); 

foreach (Microsoft.Office.Interop.Outlook.AppointmentItem c in calendarFolder.Items) 
{ 

     Console.WriteLine(c.Subject.ToString() + " " + c.Start.ToString() + " deleted"); 

     c.Delete(); 

} 

這將刪除約會,但只是夾頭的時間,如果你繼續重新運行此它最終會刪除他們... ...C#展望刪除所有約會

沒有人知道是怎麼回事,我也試圖先排序,沒有變化 -

謝謝!!

試驗後,向後循環做它 - 不知道爲什麼但是

Console.WriteLine(calendarFolder.Items.Count); 
    Microsoft.Office.Interop.Outlook.Items calendarItems = calendarFolder.Items; 
    //Microsoft.Office.Interop.Outlook.AppointmentItem app = calendarItems as Microsoft.Office.Interop.Outlook.AppointmentItem; 

    //for (int i = 1; i <= calendarFolder.Items.Count; i++) 
    for (int i = calendarFolder.Items.Count; i > 0; i--) 
    { 
     calendarFolder.Items[i].Delete(); 

     //app = calendarFolder.Items[i]; 
     Console.WriteLine(i); 
     //app.Delete(); 
    } 
+0

您是否嘗試過使用for循環通過calendarFolder.Items向後循環?即使它不能解決你的問題,它也可能提供進一步調試的線索。 – Jay 2010-09-27 19:29:46

+0

如果你做兩個foreach循環,這個問題也會發生嗎? I.e在第一個循環中,將所有AppointmentItems複製到一個數組中;在第二個循環中,刪除AppointmentItems。 – 2010-09-27 19:30:16

回答

1

這是一個循環向後固定它的原因。比方說,你已經在列表中100個元素,你檢查的循環條件每次迭代

i = 0 count = 99 
i = 1 count = 98 
     ... 
i = 50 count = 49 

for循環結束,但元素50-99仍將存在,因此爲什麼迭代向後不會導致錯誤。