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

huangapple go评论154阅读模式
英文:

How to get a File description(Product name, Original filname, etc.) using golang in windows?

问题

Fileinfo在golang中提供了名称、修改时间、大小等信息。我需要使用golang在Windows中获取特定文件的描述(例如:产品名称、原始文件名等)。

英文:

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

得分: 8

你可以使用w32库在Go语言中调用Win32 API,无需使用CGo。

以下是通过GetFileVersionInfo和VerQueryValue获取所有文件信息的示例代码:

package main

import (
	"fmt"
	"github.com/gonutz/w32/v2"
)

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\n",
		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)
}
英文:

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 (
	&quot;fmt&quot;
	&quot;github.com/gonutz/w32/v2&quot;
)

func main() {
	const path = `C:\some file`

	size := w32.GetFileVersionInfoSize(path)
	if size &lt;= 0 {
		panic(&quot;GetFileVersionInfoSize failed&quot;)
	}

	info := make([]byte, size)
	ok := w32.GetFileVersionInfo(path, info)
	if !ok {
		panic(&quot;GetFileVersionInfo failed&quot;)
	}

	fixed, ok := w32.VerQueryValueRoot(info)
	if !ok {
		panic(&quot;VerQueryValueRoot failed&quot;)
	}
	version := fixed.FileVersion()
	fmt.Printf(
		&quot;file version: %d.%d.%d.%d\n&quot;,
		version&amp;0xFFFF000000000000&gt;&gt;48,
		version&amp;0x0000FFFF00000000&gt;&gt;32,
		version&amp;0x00000000FFFF0000&gt;&gt;16,
		version&amp;0x000000000000FFFF&gt;&gt;0,
	)

	translations, ok := w32.VerQueryValueTranslations(info)
	if !ok {
		panic(&quot;VerQueryValueTranslations failed&quot;)
	}
	if len(translations) == 0 {
		panic(&quot;no translation found&quot;)
	}
	fmt.Println(&quot;translations:&quot;, translations)

	t := translations[0]
	// w32.CompanyName simply translates to &quot;CompanyName&quot;
	company, ok := w32.VerQueryValueString(info, t, w32.CompanyName)
	if !ok {
		panic(&quot;cannot get company name&quot;)
	}
	fmt.Println(&quot;company:&quot;, company)
}

1: https://github.com/gonutz/w32 "w32 library"

huangapple
  • 本文由 发表于 2017年8月23日 23:35:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/45844001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定