Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Getting Office's Version

4.17/5 (19 votes)
30 May 2008CPOL2 min read 1   4.9K  
Getting office's version by readint the app path key

Introduction

After building some office add-ins — I needed to create an installation for them. The problem was that I built add-ins for Word/Excel 2003, and add-ins for Word/Excel 2007, and I needed the installation to determine which of them to install. After doing some searching, I found out that the way to get this is by checking for the “Guid”s in the “Uninstall” registry keys. This was too exaggerated, since I only needed to know which Word/Excel version is installed. Another option was to check the office registry, but this only worked if I checked it on a clean computer, because a big portion of the keys remain after uninstalling office, so there’s still no certainty. Finally, I came up with (what I think is) a better solution, since it uses no “Guid”s of office-keys.

Background

Many of you probably know that in order to run Word/Excel/PowerPoint/Outlook, you can go to Start >> Run, and write “winword/excel/powerpnt/outlook”, this is possible due to registry keys, which contain their fullpaths. By getting the value of these keys, all I need is to get the information about the file’s version (12 for 2007, 11 for 2003).

Step I: Getting the Paths

First, for simplicity, i added an enum, to get the component wanted:

C#
public enum OfficeComponent
    {
        Word,
        Excel,
        PowerPoint,
        Outlook
    }

The keys can be found in two places, the first is under the HKEY_CURRENT_USER, and the second (if we didn’t find it already) is HKEY_LOCAL_MACHINE. The sub_path is the same on both: SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths. Now all we need is to get these values:

C#
/// <summary>
/// gets the component's path from the registry. if it can't find it - retuns 
///an empty string
/// </summary>
private string GetComponentPath(OfficeComponent _component)
{
    const string RegKey = @"Software\Microsoft\Windows\CurrentVersion\App Paths"
    string toReturn = string.Empty;
    string _key = string.Empty;

    switch (_component)
    {
        case OfficeComponent.Word:
            _key = "winword.exe";
            break;
        case OfficeComponent.Excel:
            _key = "excel.exe";
            break;
        case OfficeComponent.PowerPoint:
            _key = "powerpnt.exe";
            break;
        case OfficeComponent.Outlook:
            _key = "outlook.exe";
            break;
    }

    //looks inside CURRENT_USER:
    RegistryKey _mainKey = Registry.CurrentUser;
    try
    {
        _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, false);
        if (_mainKey != null)
        {
            toReturn = _mainKey.GetValue(string.Empty).ToString();
        }
    }
    catch
    { }

    //if not found, looks inside LOCAL_MACHINE:
    _mainKey = Registry.LocalMachine;
    if (string.IsNullOrEmpty(toReturn))
    {
        try
        {
            _mainKey = _mainKey.OpenSubKey(RegKey + "\\" + _key, false);
            if (_mainKey != null)
            {
                toReturn = _mainKey.GetValue(string.Empty).ToString();
            }
        }
        catch
        { }
    }

    //closing the handle:
    if (_mainKey != null)
        _mainKey.Close();

    return toReturn;
}

Step II: Get Files Information

Using .NET’s class FileVersionInfo, we can access many of the file’s attributes, including its version:

C#
/// <summary>
/// Gets the major version of the path. if file not found (or any other        
/// exception occures - returns 0
/// </summary>
private int GetMajorVersion(string _path)
{
    int toReturn = 0;
    if (File.Exists(_path))
    {
        try
        {
        FileVersionInfo _fileVersion = FileVersionInfo.GetVersionInfo(_path);
        toReturn = _fileVersion.FileMajorPart;
        }
        catch
        { }
    }
    return toReturn;
}

*Note: because all I need to know is if it’s 2003 or 2007, I’m only checking the major version of the file. For those of you who want to get more specific information, use the full version of the file.

Finish

Now all you have to do is to check the FileVersion of the path (i.e: int _wordVersion = GetMajorVersion(GetComponentPath(OfficeComponent.Word));: if _wordVersion = 11 – 2003, if _wordVersion = 12 – 2007. Display it nicely and you’re done.

Conclusion

This was my first article here, hope it will be useful. Thanks for dedicating time to reading it, good luck.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)