How can I programmatically find the correct Micros

2019-06-27 20:50发布

I'm having trouble finding a consistent version number for several Office products.

This post led me to these KB articles which suggest different ways to find the service packs for Office 2007 and Office 2010 products.

However, the file versions of the Office .exe files are not consistent with the chart.

Using the Excel 2010 installed on my machine as an example:

  • Help > About info from Excel: Microsoft Excel 2010 (14.0.6106.5005) SP1
  • File version by viewing the properties of Excel.exe: 14.0.6106.5005
  • Original file version (from table): 14.0.4756.1000
  • SP1 file version (from table): 14.0.6024.1000

Is there a more reliable way to retrieve version numbers and service packs for Microsoft Office products?

2条回答
地球回转人心会变
2楼-- · 2019-06-27 20:51

We decided to back out of this because it was taking way too much time. However, I thought I'd post what I got in case anyone needs to take it farther.

First, here are the three relevant KB articles which list out the service pack versions:

Method 2 in these articles suggest the properties of the executable file is a reliable way to get the actual file version. Unfortunately, it isn't.

Here's how you can find the executable:

Find the install root for 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();

Then append the name of the executable (with on exception for Office 2003):

  • Access: MSACCESS.EXE
  • Excel: EXCEL.EXE
  • Outlook: OUTLOOK.EXE (use OUTLIB.DLL for 2003)
  • PowerPoint: POWERPNT.EXE
  • Word: WINWORD.EXE

With this information, you can get the FileVersionInfo for the selected application. Using Word as an example:

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

if (fileVersionInfo != null)
    fileVersion = fileVersionInfo.FileVersion;

Theoretically, you could now compare this version number to the tables from the KB articles to find the correct service pack. This is where I abandoned my efforts for the reasons listed in the question - you'll find the version numbers just don't match up.

You can use code similar to below to compare the versions for, say Word 2010 SP1:

Version version = new Version(fileVersion);
if (version >= new Version("14.0.6024.1000"))
    servicePack = 1

Here's some code to get the version of Office suite:

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);
}

If you are trying to get the edition name (i.e. Professional, Ultimate, Student, etc) you are in for an adventure. Here's some untested code fragments that may be useful. It's different for each version of office and each edition so best of luck!

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 ", "");
查看更多
祖国的老花朵
3楼-- · 2019-06-27 20:56

See if you can adapt this code:

What is the best way to get Excel version?

This code uses Application.ProductCode to determine the Office version. ProductCode is a property available in most Office products. I checked Outlook, Word, Excel and Access 2003 and all of them had it.

You may also be able to leverage this code:

Display system and version information

This code takes the MSACCESS.exe filename and uses a Windows API to determine the version and assembly information, from which you would derive the service pack number.

查看更多
登录 后发表回答