首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试outlook是否安装了C#异常处理

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

Stack Overflow用户
提问于 2012-03-21 01:50:48
回答 2查看 2.8K关注 0票数 3

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

代码语言:javascript
复制
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();
   }

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

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

代码语言:javascript
复制
        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互操作对象是否可以工作的好方法。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-21 02:31:20

我有一台2007年安装的XP机器。因此,我不能测试所有的情况。但这段代码似乎可以工作。

代码语言:javascript
复制
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;
    }
}
票数 3
EN

Stack Overflow用户

发布于 2015-12-22 06:50:19

代码语言:javascript
复制
public bool IsOutlookInstalled()
{
    try
    {
        var officeType = Type.GetTypeFromProgID("Outlook.Application");
        if (officeType == null)
        {
            // Outlook is not installed.   
            return false;
        }
        else
        {
            // Outlook is installed.      
            return true;
        }
    }
    catch (System.Exception ex)
    {
        return false;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9792266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档