Click here to Skip to main content
6,295,667 members and growing! (12,103 online)
Email Password   helpLost your password?
Enterprise Systems » Office Development » General     Intermediate License: The Code Project Open License (CPOL)

Getting Office's Version

By trabelsy

Getting office's version by readint the app path key
C# (C# 2.0), Windows, .NET (.NET 2.0), Dev
Posted:30 May 2008
Views:5,666
Bookmarked:9 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
8 votes for this article.
Popularity: 3.29 Rating: 3.64 out of 5
1 vote, 12.5%
1
1 vote, 12.5%
2
1 vote, 12.5%
3
1 vote, 12.5%
4
4 votes, 50.0%
5

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:

/// <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:

/// <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 = 112003, if _wordVersion = 122007. 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)

About the Author

trabelsy


Member

Occupation: Software Developer
Company: StarLIMS
Location: Israel Israel

Other popular Office Development articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
Generalgood work [modified] Pinmembersubai1:38 19 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 30 May 2008
Editor: Sean Ewington
Copyright 2008 by trabelsy
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project