Testing if appearance is set with C # exception handling

I have a section of my app that allows people to send email of generated text. My current problem is that when loading a form with text, it throws an unhandled System.IO.FileNotFound exception when the user has no set appearance. When the form loads, I am trying to determine if their appearance is set.

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();
   }

      

Above is the code I tried. On the machine where I am developing, if the assembly name is disabled, the catch commands will catch it. However, when I test it on an XP machine with no perspective, it throws an error and stops loading form events.

Every catch rule I tried (Catch all did not even work):

        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"; }

      

I'm looking for a method that will work (hopefully so I could use one code to work in Outlook 2010 and 2007) without having to check the registry and the exact file path.

So some of the things I'm wondering about is why XP even throws errors and doesn't catch them, since it throws a FileNotFound when I have a catch for it, and which is a good method for determining if an object will interlook work.

+3


source to share


2 answers


I have an XP machine installed. so I couldn't check all cases. But this code works.



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


source


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


source







All Articles