问题在于:
编辑: 我知道如何在磁盘上保存联系人的图片。 问题是我得到的图片不是最新图片。 这是代码:
//[Outlook] is short for [Microsoft.Office.Interop.Outlook]
Outlook.Application outlook = new Outlook.Application();
Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
foreach (var item in folder.Items) {
if (item is Outlook.ContactItem) {
Outlook.ContactItem contact = null;
Outlook.Attachments atts = null;
Outlook.Attachment att = null;
string path = "";
contact = item as Outlook.ContactItem;
if (!contact.HasPicture) { continue; }
path = @"C:\Temp\" + contact.EntryID + ".jpg";
atts = contact.Attachments;
att = atts["ContactPicture.jpg"];
if(File.Exists(path)){
File.Delete(path);
}
att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg");
Marshal.ReleaseComObject(att);
att = null;
Marshal.ReleaseComObject(atts);
atts = null;
Marshal.ReleaseComObject(contact);
contact = null;
}
}
谢谢!
答案 0 :(得分:0)
我在Contact picture retrieved via PIA doesn't change when updated in Outlook
上找到了解决方案我没有发布folder.Items
以下是修改后的代码:
//[Outlook] is short for [Microsoft.Office.Interop.Outlook]
Outlook.Application outlook = new Outlook.Application();
Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items items = null;
items = folder.Items;
foreach (var item in items) {
if (item is Outlook.ContactItem) {
Outlook.ContactItem contact = null;
Outlook.Attachments atts = null;
Outlook.Attachment att = null;
string path = "";
contact = item as Outlook.ContactItem;
if (!contact.HasPicture) { continue; }
path = @"C:\Temp\" + contact.EntryID + ".jpg";
atts = contact.Attachments;
att = atts["ContactPicture.jpg"];
if (File.Exists(path)) {
File.Delete(path);
}
att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg");
Marshal.ReleaseComObject(att);
att = null;
Marshal.ReleaseComObject(atts);
atts = null;
Marshal.ReleaseComObject(contact);
contact = null;
}
}
if (items != null) {
Marshal.ReleaseComObject(items);
items = null;
}
编辑:
上述代码出现问题。有时它得到了旧图片。
我最后添加了GC.Collect();
,然后效果很好。
所以我尝试了以下代码。但问题仍然存在。
//[Outlook] is short for [Microsoft.Office.Interop.Outlook]
Outlook.Application outlook = new Outlook.Application();
Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items items = folder.Items;
foreach (var item in items) {
if (item is Outlook.ContactItem) {
Outlook.ContactItem contact = item as Outlook.ContactItem;
string path = "";
Outlook.Attachment att = null;
if (!contact.HasPicture) { continue; }
path = @"C:\Temp\" + contact.EntryID + ".jpg";
att = contact.Attachments["ContactPicture.jpg"];
if (File.Exists(path)) {
File.Delete(path);
}
att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg");
}
}
GC.Collect();
最后我使用以下代码。释放所有com对象,然后调用GC.Collect()。
//[Outlook] is short for [Microsoft.Office.Interop.Outlook]
Outlook.Application outlook = new Outlook.Application();
Outlook.MAPIFolder folder = outlook.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items items = folder.Items;
foreach (var item in items) {
if (item is Outlook.ContactItem) {
Outlook.ContactItem contact = null;
Outlook.Attachments atts = null;
Outlook.Attachment att = null;
string path = "";
contact = item as Outlook.ContactItem;
if (!contact.HasPicture) { continue; }
path = @"C:\Temp\" + contact.EntryID + ".jpg";
atts = contact.Attachments;
att = atts["ContactPicture.jpg"];
if (File.Exists(path)) {
File.Delete(path);
}
att.SaveAsFile(@"C:\Temp\" + contact.EntryID + ".jpg");
Marshal.ReleaseComObject(att);
att = null;
Marshal.ReleaseComObject(atts);
atts = null;
Marshal.ReleaseComObject(contact);
contact = null;
}
}
if (items != null) {
Marshal.ReleaseComObject(items);
items = null;
}
Marshal.ReleaseComObject(folder);
folder = null;
Marshal.ReleaseComObject(outlook);
outlook = null;
GC.Collect();