doumou8527 2017-08-23 15:35
浏览 179
已采纳

如何在Windows中使用golang获取文件描述(产品名称,原始文件名等)?

Fileinfo in golang gives Name, time modified, size, etc. I need to get particular File's description(eg: Product name, Original filname, etc.) using golang in windows.

  • 写回答

1条回答 默认 最新

  • doupian9798 2017-09-14 14:43
    关注

    You can use the w32 library for Win32 API calls from Go. No CGo needed.

    Here is an example of how you can retrieve all file information through GetFileVersionInfo and VerQueryValue:

    package main
    
    import (
        "fmt"
        "github.com/gonutz/w32"
    )
    
    func main() {
        const path = `C:\some file`
    
        size := w32.GetFileVersionInfoSize(path)
        if size <= 0 {
            panic("GetFileVersionInfoSize failed")
        }
    
        info := make([]byte, size)
        ok := w32.GetFileVersionInfo(path, info)
        if !ok {
            panic("GetFileVersionInfo failed")
        }
    
        fixed, ok := w32.VerQueryValueRoot(info)
        if !ok {
            panic("VerQueryValueRoot failed")
        }
        version := fixed.FileVersion()
        fmt.Printf(
            "file version: %d.%d.%d.%d
    ",
            version&0xFFFF000000000000>>48,
            version&0x0000FFFF00000000>>32,
            version&0x00000000FFFF0000>>16,
            version&0x000000000000FFFF>>0,
        )
    
        translations, ok := w32.VerQueryValueTranslations(info)
        if !ok {
            panic("VerQueryValueTranslations failed")
        }
        if len(translations) == 0 {
            panic("no translation found")
        }
        fmt.Println("translations:", translations)
    
        t := translations[0]
        // w32.CompanyName simply translates to "CompanyName"
        company, ok := w32.VerQueryValueString(info, t, w32.CompanyName)
        if !ok {
            panic("cannot get company name")
        }
        fmt.Println("company:", company)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵