如何使用C#在VSTO Word插件中获取Office品牌及完整版本号
C# VSTO Word插件获取Office完整版本号实现方案
开发面向Word的VSTO插件时,可通过以下两种方法获取和Office「账户」窗口展示完全一致的完整版本号,账户页版本号参考示例:
方法1:通过VSTO内置Application对象读取(优先推荐)
该方法无需额外权限,直接复用VSTO插件运行时的宿主实例即可拿到匹配账户页的版本号,代码示例:
using Microsoft.Office.Interop.Word; // 直接调用VSTO框架内置的Word应用实例,无需手动初始化 Application wordApp = Globals.ThisAddIn.Application; // 输出格式为「主版本.次版本.Build号.修订号」,和账户页展示的版本号完全一致 string fullOfficeVersion = wordApp.Version; // 额外补充:判断当前Office是32位还是64位 bool is64BitOffice = IntPtr.Size == 8;
适用范围:Office 2013及以上所有版本,包括Office 365/2019/2021等订阅、永久授权版本
方法2:注册表读取方案(适用于脱离VSTO上下文的场景)
如果需要在非VSTO项目中读取Office版本,可通过系统注册表查询,代码示例:
using Microsoft.Win32; string officeFullVersion = string.Empty; // 优先查询即点即用版本Office(绝大多数Office 365/2019/2021的安装方式) string regPath = @"SOFTWARE\Microsoft\Office\ClickToRun\Configuration"; using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(regPath, false)) { if (regKey != null) { officeFullVersion = regKey.GetValue("ClientVersionToReport")?.ToString(); } } // 兜底查询MSI安装格式的旧版Office if (string.IsNullOrEmpty(officeFullVersion)) { // 16.0对应Office 2016及以上版本,如需适配Office 2013可替换为15.0 regPath = @"SOFTWARE\Microsoft\Office\16.0\Common\ProductVersion"; using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(regPath, false)) { if (regKey != null) { officeFullVersion = regKey.GetValue("LastProduct")?.ToString(); } } }
注意事项:该方法需要注册表读取权限,部分企业受控环境可能被组策略拦截,优先级低于方法1
内容的提问来源于stack exchange,提问作者Trung Messi
相关产品推荐
相关产品推荐

