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

如何获取带有内嵌Info.plist的macOS可执行文件版本?

获取macOS可执行文件内嵌Info.plist版本信息的便捷方法

你可以直接利用macOS的Bundle机制读取内嵌Info.plist的可执行文件信息,无需手动扫描__info_plist段。系统会将这类可执行文件视为特殊Bundle,通过Foundation的Bundle(Objective-C中为NSBundle)接口即可直接提取版本数据。

Swift 实现示例

import Foundation

func fetchExecutableVersion(at url: URL) -> String? {
    guard let executableBundle = Bundle(url: url) else {
        return nil
    }
    // CFBundleShortVersionString 对应Finder显示的版本号,需内部版本号可改用 CFBundleVersion
    return executableBundle.infoDictionary?["CFBundleShortVersionString"] as? String
}

// 使用示例:获取/usr/bin/searchfs的版本
let searchfsPath = URL(fileURLWithPath: "/usr/bin/searchfs")
if let version = fetchExecutableVersion(at: searchfsPath) {
    print("searchfs 版本:\(version)")
} else {
    print("无法读取目标可执行文件的版本信息")
}

Objective-C 实现示例

#import <Foundation/Foundation.h>

NSString *fetchExecutableVersion(NSURL *executableURL) {
    NSBundle *executableBundle = [NSBundle bundleWithURL:executableURL];
    if (!executableBundle) {
        return nil;
    }
    // CFBundleShortVersionString 对应Finder显示的版本号,内部版本号用 CFBundleVersion
    return executableBundle.infoDictionary[@"CFBundleShortVersionString"];
}

// 使用示例
NSURL *searchfsURL = [NSURL fileURLWithPath:@"/usr/bin/searchfs"];
NSString *version = fetchExecutableVersion(searchfsURL);
if (version) {
    NSLog(@"searchfs 版本:%@", version);
} else {
    NSLog(@"无法读取目标可执行文件的版本信息");
}

说明

  • 只要可执行文件内嵌合法的__info_plist段,Bundle/NSBundle就能正确加载解析其中信息,这是系统原生支持的方式,比手动解析文件更可靠高效。
  • 若目标可执行文件未内嵌Info.plist,bundleWithURL:会返回nil,需做好容错处理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 05:15:32