65.9K
CodeProject 正在变化。 阅读更多。
Home

获取 Office 版本

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.17/5 (17投票s)

2008 年 5 月 30 日

CPOL

2分钟阅读

viewsIcon

103173

downloadIcon

4907

通过读取应用程序路径键获取 Office 版本

引言

在构建一些 Office 插件之后,我需要为它们创建一个安装程序。问题是,我为 Word/Excel 2003 和 Word/Excel 2007 构建了插件,并且我需要安装程序确定安装哪个版本。经过一番搜索,我发现可以通过检查“卸载”注册表键中的“GUID”来做到这一点。但这过于复杂了,因为我只需要知道安装了哪个 Word/Excel 版本。另一种方法是检查 Office 注册表,但这只有在干净的计算机上检查时才有效,因为在卸载 Office 后,大部分键仍然存在,因此仍然没有确定性。最终,我想到了一种(我认为)更好的解决方案,因为它不使用 Office 键的“GUID”。

背景

你们中的许多人可能知道,要运行 Word/Excel/PowerPoint/Outlook,可以转到“开始”>>“运行”,然后输入“winword/excel/powerpnt/outlook”,这是由于注册表键包含它们的完整路径。通过获取这些键的值,我只需要获取有关文件版本的信息(2007 为 12,2003 为 11)。

步骤一:获取路径

首先,为了简单起见,我添加了一个枚举,以获取所需的组件

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

这些键可以在两个地方找到,第一个是在 HKEY_CURRENT_USER 下,第二个(如果我们已经没有找到它)是在 HKEY_LOCAL_MACHINE 下。子路径在两者中都是相同的:SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths。现在我们只需要获取这些值了

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

步骤二:获取文件信息

使用 .NET 的类 FileVersionInfo,我们可以访问文件的许多属性,包括其版本

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

*注意:因为我只需要知道是 2003 还是 2007,我只检查文件的主要版本。对于那些想要获取更具体信息的人,请使用文件的完整版本。

结束

现在你只需要检查路径的 FileVersion(例如:int _wordVersion = GetMajorVersion(GetComponentPath(OfficeComponent.Word));: if _wordVersion = 112003, if _wordVersion = 122007。美观地显示它,你就完成了。

结论

这是我在这里的第一篇文章,希望对您有所帮助。感谢您抽出时间阅读,祝您好运。

© . All rights reserved.