You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#正确获取Outlook 2013-2021版本?

适用于Outlook 2013/2016/2019/2021的版本获取方案

先明确你现有方法的问题:

  • GetOfficeVersion:依赖Office Interop组件,会强制启动Outlook进程(未运行时),且需正确部署Interop程序集,否则会抛出异常;仅取主版本号的逻辑也无法满足精确版本需求。
  • GetOfficeVersionNew:仅读取默认注册表项,未兼容64位系统中32位Office的注册表路径(需读取Wow6432Node下的对应项),且CurVer的值可能因多版本共存或注册表异常导致不准确。

推荐方案1:读取Outlook.exe文件版本信息

直接读取Outlook安装目录下的Outlook.exe文件版本,这是最准确的方式,无额外依赖:

using System.Diagnostics;
using System.IO;
using Microsoft.Win32;

public static string GetOutlookVersion()
{
    string outlookPath = GetOutlookInstallPath();
    if (string.IsNullOrEmpty(outlookPath) || !File.Exists(outlookPath))
    {
        return "未知版本";
    }

    FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(outlookPath);
    // 返回完整版本号,若只需主版本号则返回versionInfo.ProductMajorPart.ToString()
    return $"{versionInfo.ProductMajorPart}.{versionInfo.ProductMinorPart}.{versionInfo.ProductBuildPart}.{versionInfo.ProductPrivatePart}";
}

private static string GetOutlookInstallPath()
{
    // 优先检查64位Office路径
    string registryPath64 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE";
    using (var key64 = Registry.LocalMachine.OpenSubKey(registryPath64, RegistryView.Registry64))
    {
        if (key64 != null)
        {
            string path = key64.GetValue("")?.ToString();
            if (!string.IsNullOrEmpty(path))
                return path;
        }
    }

    // 检查32位Office路径(64位系统下的Wow6432Node)
    string registryPath32 = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE";
    using (var key32 = Registry.LocalMachine.OpenSubKey(registryPath32, RegistryView.Registry32))
    {
        if (key32 != null)
        {
            string path = key32.GetValue("")?.ToString();
            if (!string.IsNullOrEmpty(path))
                return path;
        }
    }

    return null;
}

推荐方案2:改进注册表读取逻辑

如果倾向于用注册表,需兼容32位/64位Office,同时读取更可靠的版本项:

using Microsoft.Win32;

public static string GetOutlookVersionFromRegistry()
{
    string[] registryPaths = new[]
    {
        @"SOFTWARE\Microsoft\Office\ClickToRun\Configuration", // Click-to-Run安装(Office 365/2019/2021)
        @"SOFTWARE\Microsoft\Office\16.0\Outlook", // 2016/2019/2021
        @"SOFTWARE\Microsoft\Office\15.0\Outlook", // 2013
        @"SOFTWARE\Wow6432Node\Microsoft\Office\ClickToRun\Configuration",
        @"SOFTWARE\Wow6432Node\Microsoft\Office\16.0\Outlook",
        @"SOFTWARE\Wow6432Node\Microsoft\Office\15.0\Outlook"
    };

    foreach (var path in registryPaths)
    {
        using (var key = Registry.LocalMachine.OpenSubKey(path))
        {
            if (key == null) continue;

            // 处理Click-to-Run安装的版本标识
            string releaseId = key.GetValue("ProductReleaseIds")?.ToString();
            if (!string.IsNullOrEmpty(releaseId))
            {
                if (releaseId.Contains("2021") || releaseId.Contains("2019") || releaseId.Contains("2016"))
                    return "16.0";
                if (releaseId.Contains("2013"))
                    return "15.0";
            }

            // 处理传统MSI安装的版本信息
            string version = key.GetValue("Version")?.ToString();
            if (!string.IsNullOrEmpty(version))
            {
                return version.Split('.')[0] + ".0";
            }
        }
    }

    return "未知版本";
}

若坚持使用Interop的注意事项

务必释放COM对象,避免残留Outlook进程:

using Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;

public static string GetOutlookVersionViaInterop()
{
    Application outlookApp = null;
    try
    {
        outlookApp = new Application();
        string version = outlookApp.Version;
        return version.Split('.')[0];
    }
    catch
    {
        return "未知版本";
    }
    finally
    {
        if (outlookApp != null)
        {
            Marshal.ReleaseComObject(outlookApp);
            outlookApp = null;
        }
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
}

内容的提问来源于stack exchange,提问作者Gadzhik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 17:56:16