有没有人知道检测安装的 Office 版本的最佳方法是什么?如果安装了多个版本的 Office,我想知道它们各自的版本。另外,如果能检测安装的特定版本的 Excel,那就更好了。
有没有人知道检测安装的 Office 版本的最佳方法是什么?如果安装了多个版本的 Office,我想知道它们各自的版本。另外,如果能检测安装的特定版本的 Excel,那就更好了。
检查已安装的 Office 版本的一种方法是检查感兴趣的 Office 应用程序的 InstallRoot
注册表键。
例如,如果您想检查是否安装了 Word 2007,则应检查以下注册表键是否存在:
HKLM\Software\Microsoft\Office\12.0\Word\InstallRoot::Path
这个条目包含可执行文件的路径。
将12.0(Office 2007)替换为相应的版本号:
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 (注意,不是15.0!) Office 2013 - 15.0 Office 2016 - 16.0 Office 2019 - 16.0 (注意,不是17.0!)
其他应用程序的密钥类似:
HKLM\Software\Microsoft\Office\12.0\Excel\InstallRoot::Path
HKLM\Software\Microsoft\Office\12.0\PowerPoint\InstallRoot::Path
或者你可以检查所有应用程序的常见根路径:
HKLM\Software\Microsoft\Office\12.0\Common\InstallRoot::Path
另一个选择,不使用特定的注册表键,可以通过使用MSIEnumProducts
API查询MSI数据库来实现,具体描述可以参考这里。
顺便提一下,微软官方不支持在同一台计算机上并行安装不同版本的Office。尽管有些情况它们可能能够运作,但你可能会遇到意料之外的问题和不一致性。
更新:Office 2019和Office 365
从Office 2019开始,不再提供基于MSI的安装程序,现在唯一的部署Office的方式是使用Click-To-Run。随着这种变化朝向定期更新的Office 365,Office的主/次版本号也不再更新(至少目前是这样)。这意味着即使对于Office 2019,用于注册表键的值和Application.Version
返回的值(例如在Word中)仍然为16.0
。
目前还没有记录的方法来区分Office 2016和Office 2019。一个提示可能是winword.exe的文件版本;但是,这个版本对于补丁的Office 2016版本也会增加(请参见@antonio的评论)。
如果你需要在Office版本之间进行区分,例如确保某个特定功能存在或已安装Office的最低版本,请查看其中一个主要Office应用程序的文件版本,可能是最好的方法:
// 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\root\Office16\WINWORD.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);
Office 2019的文件版本为16.0.10730.20102,因此如果您看到任何大于该版本号的版本,您正在处理Office 2019或当前的Office 365版本。
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Office
中查找。 - Dirk VollmarHKEY_CLASSES_ROOT\Word.Application\CurVer怎么样?
Word.Application.15
等等。它不会返回文件或注册表路径。你仍然需要单独获取那些信息。 - ChrisComputer\HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Common\Licensing\LicensingNext
下。因此,我的逻辑如下:
情况1:如果计算机安装了MSOffice 2016,则在Licensing
下没有子键。
情况2:如果计算机安装了MSOffice 2019,则会显示一个值的名称(即Office产品ID之一)。 (例如Standard2019Volume
)
案例3:如果计算机安装了Office365,则会有一个名为o365bussinessretail
(也是产品ID)的值以及其他一些值。LicensingNext
并尝试查看任何名称是否具有前缀o365
,如果找到它,则是O365。如果没有,则是Office 2019。namespace Software_Info_v1._0
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop;
public class MS_Office
{
public string GetOfficeVersion()
{
string sVersion = string.Empty;
Microsoft.Office.Interop.Word.Application appVersion = new Microsoft.Office.Interop.Word.Application();
appVersion.Visible = false;
switch (appVersion.Version.ToString())
{
case "7.0":
sVersion = "95";
break;
case "8.0":
sVersion = "97";
break;
case "9.0":
sVersion = "2000";
break;
case "10.0":
sVersion = "2002";
break;
case "11.0":
sVersion = "2003";
break;
case "12.0":
sVersion = "2007";
break;
case "14.0":
sVersion = "2010";
break;
default:
sVersion = "Too Old!";
break;
}
Console.WriteLine("MS office version: " + sVersion);
return null;
}
}
}
Globals.ThisAddIn.Application.Version
。 - Fabricio我找到了一种优雅的方法来检测“Microsoft Office版本”,也许在您想要检测是否安装了“Office 2016”或“Office 2019”时也适用。
我只是检测了“Microsoft Office”的安装路径,然后从“Office”应用程序(例如“Word”)的exe文件中声明了一个System.Diagnostics.FileVersionInfo,并从这个FileVersionInfo中获取了我需要的所有内容。
以下是一个非常小的控制台应用程序的完整代码示例:
using System;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Sandbox_Console
{
public class Program
{
static void Main(string[] args)
{
string installpath = GetOfficeInstallPath();
OfficeVersionInfo info = new OfficeVersionInfo(installpath);
Console.WriteLine("Full Office Version Number: " + info.FullVersionNumber);
Console.WriteLine("Full Office Name: " + info.FullOfficeVersionName);
Console.Write("Press any key to end this program...");
Console.ReadKey();
}
public class OfficeVersionInfo
{
private string _FullVersionNumber = "";
private string _FullOfficeVersionName = "";
public string FullVersionNumber { get { return _FullVersionNumber; } }
public string FullOfficeVersionName { get { return _FullOfficeVersionName; } }
public OfficeVersionInfo(string installPath)
{
string filepath = installPath + "\\winword.exe"; //For Excel, PowerPoint or others use the exe-files for them.
if (File.Exists(filepath))
{
FileVersionInfo info = FileVersionInfo.GetVersionInfo(filepath);
_FullVersionNumber = info.ProductVersion;
_FullOfficeVersionName = info.ProductName;
}
}
}
public static string GetOfficeInstallPath()
{
string result = "";
try
{
Type appType = Type.GetTypeFromProgID("Word.Application"); //Also works with Excel, PowerPoint or others.
dynamic appInstance = Activator.CreateInstance(appType);
result = appInstance.Path;
appInstance.quit();
Marshal.ReleaseComObject(appInstance);
} catch (Exception exc)
{
Debug.Print(exc.Message);
}
return result;
}
}
}
"OfficeVersionInfo" 里存储的信息是当你右键点击 "winword.exe",然后点击 "属性" 时所得到的信息。请参见: winword.exe 的详细信息
winword.exe
)的 Product name
属性仅显示 Microsoft Office
,没有关于具体版本的更多信息。 - Unhandled Exception我知道这个问题很久以前就被问过和回答了,但是直到我做出以下观察,这个问题才让我忙碌:
要获取构建号(例如15.0.4569.1506
),请探查HKLM\SOFTWARE\Microsoft\Office\[VER]\Common\ProductVersion::LastProduct
,其中[VER]
是主要版本号(Office 2007为12.0,Office 2010为14.0,Office 2013为15.0)。
在64位Windows上,无论Office安装的位数如何,都需要在SOFTWARE
和Microsoft
之间插入Wow6432Node
。
在我的机器上,这提供了最初安装版本的版本信息。例如,对于Office 2010,数字与此处列出的数字匹配,并且它们与文件> 帮助
中报告的版本不同,后者反映了通过热修复应用的补丁。
public string WinWordVersion
{
get
{
string _version = string.Empty;
Word.Application WinWord = new Word.Application();
switch (WinWord.Version.ToString())
{
case "7.0": _version = "95";
break;
case "8.0": _version = "97";
break;
case "9.0": _version = "2000";
break;
case "10.0": _version = "2002";
break;
case "11.0": _version = "2003";
break;
case "12.0": _version = "2007";
break;
case "14.0": _version = "2010";
break;
case "15.0": _version = "2013";
break;
case "16.0": _version = "2016";
break;
default:
break;
}
return WinWord.Caption + " " + _version;
}
}
ManagementObjectSearcher LicenseSearcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT name, LicenseStatus,Description,PartialProductKey FROM SoftwareLicensingProduct where name like '%office%'");
foreach (ManagementObject LSObj in LicenseSearcher.Get())
{
String prod_name = LSObj["Name"].ToString();
String prod_description = LSObj["Description"].ToString();
String prod_key = "";
try
{
prod_key = LSObj["PartialProductKey"].ToString();
}
catch
{
prod_key = "";
}
String prod_lic_sta= LSObj["LicenseStatus"].ToString();
switch (prod_lic_sta)
{
case "0":
prod_lic_sta = "Unlicensed";
break;
case "1":
prod_lic_sta = "Licensed";
break;
case "2":
prod_lic_sta = "OOBGrace";
break;
case "3":
prod_lic_sta = "OOTGrace";
break;
case "4":
prod_lic_sta = "NonGenuineGrace";
break;
case "5":
prod_lic_sta = "Notification";
break;
case "6":
prod_lic_sta = "ExtendedGrace";
break;
}
}
输出:
Office 19
Office19HomeBusiness2019R_Retail版
密钥:XXXXX
状态:已授权