c# - 测试 outlook 是否安装了 C# 异常处理

标签 c# exception outlook

我的应用程序有一部分允许人们将生成的文本发送到电子邮件中。我目前的问题是,当他们用文本加载表单时,当用户没有安装 outlook 时,它会抛出一个未处理的异常 System.IO.FileNotFound。在加载表单时,我尝试确定他们是否安装了 Outlook。

try{
       //Assembly _out = Assembly.Load("Microsoft.Office.Interop.Outlook");
       Assembly _out = Assembly.Load("Microsoft.Office.Interop.Outlook, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");
       //Microsoft.Office.Interop.Outlook.Application _out = new Microsoft.Office.Interop.Outlook.Application();
       //Microsoft.Office.Interop.Outlook.Application _out = new Microsoft.Office.Interop.Outlook.Application();
   }

上面是我一直在尝试的代码。在我正在开发的计算机上,如果程序集名称不存在,catch 语句将捕获它。但是,当我在没有 outlook 的 XP 机器上测试它时,它会抛出错误并停止加载表单事件。

我尝试过的每个 catch 语句(Catch all 甚至都不起作用):

        catch (System.IO.FileLoadException)
        { _noOutlook = true; type = "FILE-LOAD"; }
        catch (System.IO.FileNotFoundException)
        { _noOutlook = true; type = "FILE-NOT-FOUND"; }
        catch (System.IO.IOException)
        { _noOutlook = true; type = "IO"; }
        catch (System.Runtime.InteropServices.COMException)
        { _noOutlook = true; type = "INTEROP"; }
        catch (System.Runtime.InteropServices.InvalidComObjectException)
        { _noOutlook = true; type = "INTEROP-INVALIDCOM"; }
        catch (System.Runtime.InteropServices.ExternalException)
        { _noOutlook = true; type = "INTEROP-EXTERNAL"; }
        catch (System.TypeLoadException)
        { _noOutlook = true; type = "TYPELOAD"; }
        catch (System.AccessViolationException)
        { _noOutlook = true; type = "ACCESVIOLATION"; }
        catch (WarningException)
        { _noOutlook = true; type = "WARNING"; }
        catch (ApplicationException)
        { _noOutlook = true; type = "APPLICATION"; }
        catch (Exception)
        { _noOutlook = true; type = "NORMAL"; }

我正在寻找一种无需检查注册表/确切文件路径即可工作的方法(希望这样我可以使用一个代码来处理 Outlook 2010 和 2007)。

所以我想知道的几件事是为什么 XP 甚至抛出错误而不捕获它们,因为当我有一个捕获它时它抛出 FileNotFound,以及确定 outlook 互操作对象是否会的好方法是什么工作。

最佳答案

我有一台 2007 安装的 XP 机器。因此我无法测试所有情况。但这段代码似乎有效。

public static bool IsOutlookInstalled()
{
    try
    {
        Type type = Type.GetTypeFromCLSID(new Guid("0006F03A-0000-0000-C000-000000000046")); //Outlook.Application
        if (type == null) return false;
        object obj = Activator.CreateInstance(type);
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
        return true;
    }
    catch (COMException)
    {
        return false;
    }
}

关于c# - 测试 outlook 是否安装了 C# 异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9792266/

相关文章:

c# - 为什么.NET 框架不提供深度复制对象的方法?

c# - 如何在字典中插入第一个元素?

c# - SQL批量复制内存问题

C++ - 捕获双重异常

regex - 替换事件电子邮件草稿正文中的字符串

c# - 以编程方式创建包时,Enterprise Architect 不会在浏览器中显示包

javascript - 获取哪个函数/行在 javascript 中抛出异常

Python 等同于 Java Exception with cause

vba - 为什么创建邮件时要在.Body 中添加一行?

excel - VBA 应用程序定义的错误 Outlook 连接