如何以编程方式查找正确的Microsoft Office产品版本号(和Service Pack)?

Jas*_*son 4 .net ms-office

我无法为多个Office产品找到一致的版本号.

这篇文章引导我阅读这些知识库文章,这些文章提出了查找Office 2007Office 2010产品服务包的不同方法.

但是,Office .exe文件的文件版本与图表不一致.

以我的机器上安装的Excel 2010为例:

  • 帮助>关于Excel中的信息:Microsoft Excel 2010(14.0.6106.5005)SP1
  • 查看Excel.exe属性的文件版本:14.0.6106.5005
  • 原始文件版本(来自表格):14.0.4756.1000
  • SP1文件版本(来自表):14.0.6024.1000

是否有更可靠的方法来检索Microsoft Office产品的版本号和服务包?

Jas*_*son 6

我们决定退出这个,因为它花了太多时间.但是,我想我会发布我得到的东西以防任何人需要更远的地方.

首先,以下是列出Service Pack版本的三篇相关知识库文章:

这些文章中的方法2表明可执行文件的属性是获取实际文件版本的可靠方法.不幸的是,事实并非如此.

以下是您可以找到可执行文件的方法:

找到Office的安装根目录:

// version is one of these three: Office 2003 = 11, Office 2007 = 12, Office 2010 = 14
RegistryKey registryKey = 
    Registry.LocalMachine.OpenSubKey(String.Format(
        @"SOFTWARE\Microsoft\Office\{0}.0\Common\InstallRoot", (int)version));

if (registryKey == null)
    registryKey = Registry.LocalMachine.OpenSubKey(
        registryKeyPath.Insert("SOFTWARE".Length, "\\Wow6432Node"));

if (registryKey != null)
    installRoot = registryKey.GetValue("Path").ToString();
Run Code Online (Sandbox Code Playgroud)

然后附加可执行文件的名称(Office 2003的例外情况):

  • 访问:MSACCESS.EXE
  • Excel:EXCEL.EXE
  • Outlook:OUTLOOK.EXE(使用OUTLIB.DLL for 2003)
  • PowerPoint:POWERPNT.EXE
  • 单词:WINWORD.EXE

使用此信息,您可以获取所选应用程序的FileVersionInfo.以Word为例:

FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(
    Path.Combine(installRoot, "WINWORD.EXE"));

if (fileVersionInfo != null)
    fileVersion = fileVersionInfo.FileVersion;
Run Code Online (Sandbox Code Playgroud)

从理论上讲,您现在可以将此版本号与KB文章中的表进行比较,以找到正确的Service Pack.这是我因为问题中列出的原因而放弃我的努力的地方 - 你会发现版本号不匹配.

您可以使用类似于下面的代码来比较 Word 2010 SP1的版本:

Version version = new Version(fileVersion);
if (version >= new Version("14.0.6024.1000"))
    servicePack = 1
Run Code Online (Sandbox Code Playgroud)

以下是获取Office套件版本的一些代码:

string msodllPath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
    String.Format(@"Common Files\microsoft shared\OFFICE{0}\MSO.DLL", (int)Version));

if (!File.Exists(msodllPath))
    msodllPath = msodllPath.Replace("Program Files", "Program Files (x86)");

if (File.Exists(msodllPath))
{
    FileVersionInfo msodll = FileVersionInfo.GetVersionInfo(msodllPath);
    FileVersion = new Version(msodll.FileVersion);
}
Run Code Online (Sandbox Code Playgroud)

如果你想获得版本名称(即专业版,终极版,学生版等),那么你就是在冒险.这是一些可能有用的未经测试的代码片段.每个版本的办公室和每个版本都有所不同,所以祝你好运!

string fullNameRegistryKey = "";

if (Version == OfficeVersion.Office2010)
    fullNameRegistryKey = String.Format(
        @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Office{0}.PROPLUSR",
        (int)Version);
else if (Version == OfficeVersion.Office2007)
    fullNameRegistryKey = String.Format(
        @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\PRO",
       (int)Version);

RegistryKey installRootRegistryKey = GetSoftwareRegistryKey(fullNameRegistryKey);

if (installRootRegistryKey != null)
    FullName = installRootRegistryKey.GetValue("DisplayName")
        .ToString().Replace("Microsoft ", "");
Run Code Online (Sandbox Code Playgroud)