我有一段允許用戶發送生成文本的電子郵件的應用程序。我目前的問題是,當他們用文本加載表單時,當用戶沒有安裝Outlook時,它會引發未處理的異常System.IO.FileNotFound。在表單的負載上,我嘗試確定是否安裝了Outlook。測試Outlook是否安裝了C#異常處理
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 (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的Outlook 2010和XP的Windows 7。 – UnholyRanger 2012-03-21 13:18:36