如何检测已安装的 MS-Office 版本? [英] How to detect installed version of MS-Office?

查看:61
本文介绍了如何检测已安装的 MS-Office 版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道检测安装了哪个版本的 Office 的最佳方法是什么?另外,如果安装了多个版本的 Office,我想知道它们是什么版本.如果我能检测到已安装的特定 Excel 版本,那将是一个奖励.

解决方案

检查安装的 Office 版本的一种方法是检查感兴趣的 Office 应用程序的 InstallRoot 注册表项.

>

例如,如果您想检查是否安装了 Word 2007,您应该检查是否存在以下注册表项:

HKLMSoftwareMicrosoftOffice12.0WordInstallRoot::Path

此条目包含可执行文件的路径.

用相应的版本号替换 12.0(对于 Office 2007):

<前>办公室 97 - 7.0办公室 98 - 8.0办公室 2000 - 9.0Office XP - 10.0办公室 2003 - 11.0办公室 2007 - 12.0Office 2010 - 14.0(原文如此!)办公室 2013 - 15.0办公室 2016 - 16.0Office 2019 - 16.0(原文如此!)

其他应用程序有类似的键:

HKLMSoftwareMicrosoftOffice12.0ExcelInstallRoot::PathHKLMSoftwareMicrosoftOffice12.0PowerPointInstallRoot::Path

或者你可以查看所有应用的公共根路径:

HKLMSoftwareMicrosoftOffice12.0CommonInstallRoot::Path

另一种不使用特定注册表项的选项是使用 MSIEnumProducts API 查询 MSI 数据库,如这里.

顺便说一句,Microsoft 不正式支持不同 Office 版本的并行安装.它们确实有些作用,但您可能会得到不想要的效果和不一致.

更新:Office 2019 和 Office 365

从 Office 2019 开始,基于 MSI 的设置不再可用,即点即用是现在部署 Office 的唯一方法.随着对定期更新的 Office 365 的这种更改,Office 的主要/次要版本号也不再更新(至少目前是这样).这意味着——即使对于 Office 2019——注册表项中使用的值和 Application.Version(例如在 Word 中)返回的值仍然是 16.0.

暂时没有记录的方法来区分 Office 2016 和 Office 2019.线索可能是 winword.exe 的文件版本;但是,此版本也会针对打过补丁的 Office 2016 版本增加(请参阅下面@antonio 的评论).

如果您需要以某种方式区分 Office 版本,例如确保存在某个功能或安装了最低版本的 Office,这可能是查看其中一个主要 Office 应用程序的文件版本的最佳方式:

//使用winword.exe的文件路径//检索路径,例如从 InstallRoot 注册表项var fileVersionInfo = FileVersionInfo.GetVersionInfo(@"C:Program Files (x86)Microsoft Office
ootOffice16WINWORD.EXE");var version = new Version(fileVersionInfo.FileVersion);//在使用 `Process` 类的运行实例上var process = Process.GetProcessesByName("winword").First();字符串 fileVersionInfo = process.MainModule.FileVersionInfo.FileVersion;var version = Version(fileVersionInfo);

Office 2019 的文件版本为 16.0.10730.20102,因此如果您看到任何大于此版本的内容,您正在处理的是 Office 2019 或当前的 Office 365 版本.

Does anyone know what would be the best way to detect which version of Office is installed? Plus, if there are multiple versions of Office installed, I'd like to know what versions they are. A bonus would be if I can detect the specific version(s) of Excel that is(/are) installed.

解决方案

One way to check for the installed Office version would be to check the InstallRoot registry keys for the Office applications of interest.

For example, if you would like to check whether Word 2007 is installed you should check for the presence of the following Registry key:

HKLMSoftwareMicrosoftOffice12.0WordInstallRoot::Path

This entry contains the path to the executable.

Replace 12.0 (for Office 2007) with the corresponding version number:

Office 97   -  7.0
Office 98   -  8.0
Office 2000 -  9.0
Office XP   - 10.0
Office 2003 - 11.0
Office 2007 - 12.0
Office 2010 - 14.0 (sic!)
Office 2013 - 15.0
Office 2016 - 16.0
Office 2019 - 16.0 (sic!)

The other applications have similar keys:

HKLMSoftwareMicrosoftOffice12.0ExcelInstallRoot::Path
HKLMSoftwareMicrosoftOffice12.0PowerPointInstallRoot::Path

Or you can check the common root path of all applications:

HKLMSoftwareMicrosoftOffice12.0CommonInstallRoot::Path

Another option, without using specific Registry keys would be to query the MSI database using the MSIEnumProducts API as described here.

As an aside, parallel installations of different Office versions are not officially supported by Microsoft. They do somewhat work, but you might get undesired effects and inconsistencies.

Update: Office 2019 and Office 365

As of Office 2019, MSI-based setup are no longer available, Click-To-Run is the only way to deploy Office now. Together with this change towards the regularly updated Office 365, also the major/minor version numbers of Office are no longer updated (at least for the time being). That means that – even for Office 2019 – the value used in Registry keys and the value returned by Application.Version (e.g. in Word) still is 16.0.

For the time being, there is no documented way to distinguish the Office 2016 from Office 2019. A clue might be the file version of the winword.exe; however, this version is also incremented for patched Office 2016 versions (see the comment by @antonio below).

If you need to distinguish somehow between Office versions, e.g. to make sure that a certain feature is present or that a minimum version of Office is installed, probably the best way it to look at the file version of one of the main Office applications:

// Using the file path to winword.exe
// Retrieve the path e.g. from the InstallRoot Registry key
var fileVersionInfo = FileVersionInfo.GetVersionInfo(@"C:Program Files (x86)Microsoft Office
ootOffice16WINWORD.EXE");
var version = new Version(fileVersionInfo.FileVersion);

// On a running instance using the `Process` class
var process = Process.GetProcessesByName("winword").First();
string fileVersionInfo = process.MainModule.FileVersionInfo.FileVersion;
var version = Version(fileVersionInfo);

The file version of Office 2019 is 16.0.10730.20102, so if you see anything greater than that you are dealing with Office 2019 or a current Office 365 version.

这篇关于如何检测已安装的 MS-Office 版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆