我的应用程序中有一个部分,允许人们发送生成的文本的电子邮件。我目前的问题是,当他们加载带有文本的表单时,当用户没有安装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();
}上面是我一直在尝试的代码。在我正在开发的计算机上,如果程序集名称为off,则catch语句将捕获它。但是,当我在没有outlook的XP计算机上测试它时,它抛出错误并停止加载表单事件。
我尝试过的每个catch语句(Catch all都不起作用):
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互操作对象是否可以工作的好方法。
发布于 2012-03-21 02:31:20
我有一台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;
}
}发布于 2015-12-22 06:50:19
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;
}
}https://stackoverflow.com/questions/9792266
复制相似问题