MailItem.PropertyAccessor和Marshal.ReleaseComObject()VSTO [英] MailItem.PropertyAccessor and Marshal.ReleaseComObject() VSTO

查看:71
本文介绍了MailItem.PropertyAccessor和Marshal.ReleaseComObject()VSTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在.msg文件中保存了一个带有userproperty的MailItem。


如果outlook打开文件,我会读PropertyAccessor的财产:


           试试
            {

                string prop =" http://schemas.microsoft.com/mapi/string/ {00020329-0000-0000-C000-000000000046} /" +

                              propName;

                object value = _mail.PropertyAccessor.GetProperty(prop);

                res = value.ToString();
$


            }¥b $ b            catch(例外)

            {

                //忽略

            }¥b $ b           终于

            {

                Marshal.ReleaseComObject(_mail.PropertyAccessor);

            }




它工作正常。


但Outlook不会发布。 msg文件。所以,如果我第二次尝试打开.msg文件,我会收到消息,该文件是用另一个程序打开的。


如何才能获得该文件?


感谢您的建议


Konstantin


解决方案

Hello Konstantin,


手动将磁盘保存在磁盘上时,是否会得到相同的结果?


首先,您需要立即释放所有底层COM对象。
使用 
System.Runtime.InteropServices.Marshal.ReleaseComObject &bbsp;
在您使用完毕后发布Outlook对象。 然后s
et变量为  没有  
Visual Basic(
null   以释放对象的引用。在
$ b中了解更多相关信息。 $ b系统释放对象文章。


我注意到以下几行代码:

 object value = _mail.PropertyAccessor.GetProperty(prop); 

MailItem类的PropertyAccessor属性返回应该在之后释放的相应类的实例。


以下语句与释放它无关:

 Marshal.ReleaseComObject(_mail.PropertyAccessor); 

相反,它会获得一个新实例。每次致电酒店时,参考计数器都会增加。因此,您需要存储在任何对象引用中,然后使用它来释放。




Hi,

I save one MailItem with a userproperty in .msg file.

If outlook opens the file, I read the property with PropertyAccessor:

           try
            {
                string prop = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/" +
                              propName;
                object value = _mail.PropertyAccessor.GetProperty(prop);
                res = value.ToString();

            }
            catch (Exception)
            {
                //ignore
            }
            finally
            {
                Marshal.ReleaseComObject(_mail.PropertyAccessor);
            }


It works fine.

But Outlook don't release the .msg file. So, if I try to open the .msg file second time, I get the message, that the file is open with another program.

How can I get the file free?

Thanks in advice

Konstantin

解决方案

Hello Konstantin,

Do you get the same results when you save the Outlook item on the disk manually?

First of all, you need to release all underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it.  Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article.

I have noticed the following lines of code:

 object value = _mail.PropertyAccessor.GetProperty(prop);

The PropertyAccessor property of the MailItem class returns an instance of the corresponding class which should be released after.

The following statement is not related to releasing it:

Marshal.ReleaseComObject(_mail.PropertyAccessor);

Instead, it gets a new instance. The reference counter is increased each time you call the property. So, you need to store in any object reference and then use it for releasing.


这篇关于MailItem.PropertyAccessor和Marshal.ReleaseComObject()VSTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆