Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
测试outlook是否安装了C#异常处理_C#_Exception Handling_Outlook - Fatal编程技术网

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

测试outlook是否安装了C#异常处理,c#,exception-handling,outlook,C#,Exception Handling,Outlook,我的应用程序中有一个部分,允许人们发送生成文本的电子邮件。我当前的问题是,当他们加载带有文本的表单时,当用户没有安装outlook时,它会抛出一个未处理的异常System.IO.FileNotFound。在加载表单时,我尝试确定是否安装了outlook try{ //Assembly _out = Assembly.Load("Microsoft.Office.Interop.Outlook"); Assembly _out = Assembly.Load("Micr

我的应用程序中有一个部分,允许人们发送生成文本的电子邮件。我当前的问题是,当他们加载带有文本的表单时,当用户没有安装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语句中,则catch语句将捕获它。但是,当我在没有outlook的XP机器上测试它时,它会抛出错误并停止加载表单事件

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

我正在寻找一种不必检查注册表/确切文件路径就可以工作的方法(希望这样我可以使用一个代码为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;
    }
}

谢谢你,这很有效。它在装有Outlook 2010的Windows 7和不带Outlook的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;
    }
}
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;
    }
}