I need to know whether Microsoft Word, Excel, Outlook, Project, etc are installed in a Windows Forms .net 2.0 C# application.
The first attempt was by simply trying to create the application objects and catching any exception, but this is too time consuming.
Is there any faster way to detect this? Like checking registry values, or another technique with the COM wrappers?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You can use the MSI (Windows Installer) APIs to find out if the relevant product/package/component codes are present on the machine. These are fairly simple to use via P/Invoke.
Alternatively, you can look in the registry. Word 2007, for example, puts its install location at HKLM\SOFTWARE\Microsoft\Office\12.0\Word\InstallRoot
.
This doesn't help you if you're planning on using the interop components, but it does tell you, with reasonable certainty, whether the various things are installed.
回答2:
This should work, as described here. It's not a very elegant solution, though, as it is version specific and will break with the next office version. This example is forOffice 2003, so it won't work with Office 2007 without updating.
const string ASSEMBLY2003 = "Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c";
static bool IsAssemblyInstalled(string assembly)
{
try
{
s_assemblyExcel = Assembly.Load(assembly);
return true;
}
catch
{
return false;
}
}