编程语言
首页 > 编程语言> > 测试Outlook是否通过C#异常处理安装

测试Outlook是否通过C#异常处理安装

作者:互联网

我的应用程序中有一个部分,允许人们发送生成的文本的电子邮件.我当前的问题是,当用户使用文本加载表单时,如果用户未安装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全部都没有用):

        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 Interop对象是否有效的好方法.

解决方法:

我有一台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;
    }
}

标签:exception-handling,outlook,c
来源: https://codeday.me/bug/20191201/2081898.html