Click here to Skip to main content
13,868,406 members
Click here to Skip to main content
Add your own
alternative version

Stats

82.8K views
4.6K downloads
30 bookmarked
Posted 30 May 2008
Licenced CPOL

Getting Office's Version

, 30 May 2008
Rate this:
Please Sign up or sign in to vote.
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:

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:

/// <span class="code-SummaryComment"><summary></span>
/// gets the component's path from the registry. if it can't find it - retuns 
///an empty string
/// <span class="code-SummaryComment"></summary></span>
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:

/// <span class="code-SummaryComment"><summary></span>
/// Gets the major version of the path. if file not found (or any other        
/// exception occures - returns 0
/// <span class="code-SummaryComment"></summary></span>
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)

Share

About the Author

Niskov
Software Developer StarLIMS
Israel Israel
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
QuestionOffice 365 Pin
HStrix28-Jan-16 8:28
memberHStrix28-Jan-16 8:28 
SuggestionOfficial Registry key? Pin
Khaari29-Oct-13 22:20
professionalKhaari29-Oct-13 22:20 
GeneralMy vote of 5 Pin
jawed.ace16-May-11 1:36
memberjawed.ace16-May-11 1:36 
GeneralMy vote of 5 Pin
btszyt22-Apr-11 5:27
memberbtszyt22-Apr-11 5:27 
GeneralMy vote of 5 Pin
Vladimir Svyatski28-Feb-11 23:48
professionalVladimir Svyatski28-Feb-11 23:48 
GeneralMy vote of 4 Pin
pintel20-Jan-11 2:26
memberpintel20-Jan-11 2:26 
GeneralMy vote of 3 Pin
MUGABI ROBERT4-Oct-10 4:58
memberMUGABI ROBERT4-Oct-10 4:58 
GeneralVery fine Pin
happybytes15-Apr-10 5:26
memberhappybytes15-Apr-10 5:26 
GeneralGood Work Pin
imgen12-Oct-09 3:43
memberimgen12-Oct-09 3:43 
Generalgood work [modified] Pin
subai19-Oct-08 1:38
membersubai19-Oct-08 1: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190214.1 | Last Updated 30 May 2008
Article Copyright 2008 by Niskov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid