动态加载和使用COM对象在C# [英] Dynamically load and use COM object in C#

查看:191
本文介绍了动态加载和使用COM对象在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#项目,其中我想访问MS outlook,如果它安装在客户端的机器上。 访问外观部分通过引用外观COM对象,并从那里去。我的问题现在是如果它是安装部分。目前,我的项目不会在没有安装outlook的机器上编译,所以我认为我不会引用outlook组件,而是加载和使用它动态,在检测到前景存在,但我没有找到了一种方法来做到这一点。

I have a C# project, where I would like to access MS outlook, if it is installed on a client´s machine. The "access outlook" part has been done by referencing the outlook COM object, and going from there. My problem is now the "if it is installed" part. At the moment, my project doesn´t compile on machines without outlook installed, so I assume that I will have to not reference the outlook component, and instead load and use it dynamically, after detecting that outlook is present, but I haven´t found a way to do this. Am I correct, and does anybody have any hints on how to do this?

感谢。

编辑:解决。根据Hans Passant在关于使用办公室PIA的评论之一提出的建议,被证明是最小阻力的路径。我在我的办公室机器上获得PIA有点困难,但它是克服使用接受的答案

Resolved. Following the advice given by Hans Passant in one of the comments about using the office PIAs, proved to be the path of least resistance. I had a little difficulty getting the PIAs on my office-less machine, but it was overcome using the accepted answer to this question.

推荐答案

遵循Hans Passant的建议,我现在回答我自己的问题,我的解决方案的细节。我能够在没有安装办公室的计算机上编译和运行我的项目。主要的问题是让visual studio知道,它可以期望从COM对象的接口。这是通过为outlook找到主接口程序集(PIA)文件来解决的。该过程在问题,但简短的故事是,我已经在我的电脑上的办公室PIA,在这个位置:

Following Hans Passant's advice, I now answer my own question with details of my solution. I was able to both compile and run my project on a computer without office installed. The primary problem was to let visual studio know, which interface it could expect from the COM object. This was solved by finding a primary interface assembly (PIA) file for outlook. That process is better described in this question, but the short story is that I already had the office PIAs on my computer, in this location:

C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA

VS2010 pro,我不知道文件是否包括其他版本。然后将相关文件复制到我的项目源文件夹,包括它在我的项目和源代码控制,并引用它。之后,我能够在我的项目中使用Outlook COM类型,并编译它没有错误。我也可以运行我的项目没有办公室安装。这是通过尝试实例化相关的COM对象,并捕获例外,如果没有安装办公室。以下示例代码显示了如何:

I use VS2010 pro, I don't know if the files are included with other versions. It was then a matter of copying the relevant file to my project source folder, including it in my project and source control, and making a reference to it. After that, I was able to use Outlook COM types in my project and compile it without error. I am also able to run my project without office installed. This is achieved by trying to instantiate the relevant COM objects, and catching the exception if office is not installed. The following sample code shows how:

using Outlook = Microsoft.Office.Interop.Outlook;

namespace Foo
{
    public class Bar
    {
        public void Quux()
        {
            try
            {
                // try to instantiate outlook COM object.
                Outlook.Application outlookApp = new Outlook.Application();

                // if it works, fine. Proceed
                ...
            }
            // If we catch a COMException, assume no office installed. Deal accordingly.
            catch (System.Runtime.InteropServices.COMException)
            {
                ...
            }
        }
    }
}

这篇关于动态加载和使用COM对象在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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