Click here to Skip to main content
15,886,919 members
Articles / Programming Languages / C#
Article

Getting Office's Version

Rate me:
Please Sign up or sign in to vote.
4.17/5 (19 votes)
30 May 2008CPOL2 min read 101.5K   4.9K   30   10
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)


Written By
Software Developer StarLIMS
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionOffice 365 Pin
HStrix28-Jan-16 7:28
HStrix28-Jan-16 7:28 
SuggestionOfficial Registry key? Pin
Khaari29-Oct-13 21:20
professionalKhaari29-Oct-13 21:20 
GeneralMy vote of 5 Pin
jawed.ace16-May-11 0:36
jawed.ace16-May-11 0:36 
GeneralMy vote of 5 Pin
btszyt22-Apr-11 4:27
btszyt22-Apr-11 4:27 
GeneralMy vote of 5 Pin
Vladimir Svyatski28-Feb-11 22:48
professionalVladimir Svyatski28-Feb-11 22:48 
GeneralMy vote of 4 Pin
pintel20-Jan-11 1:26
pintel20-Jan-11 1:26 
GeneralMy vote of 3 Pin
MUGABI ROBERT4-Oct-10 3:58
MUGABI ROBERT4-Oct-10 3:58 
GeneralVery fine Pin
happybytes15-Apr-10 4:26
happybytes15-Apr-10 4:26 
GeneralGood Work Pin
imgen12-Oct-09 2:43
imgen12-Oct-09 2:43 
Generalgood work [modified] Pin
subai19-Oct-08 0:38
subai19-Oct-08 0:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.