What is the best way to get windows full name from go?

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

What is the best way to get windows full name from go?

问题

在Windows中,我可以运行类似于systeminfo | findstr /C:"OS Name"的命令来将Windows的完整名称输出到控制台。我尝试了几种不同的将一个命令的输出传递给另一个命令的方法,但是我只得到了空字符串。

有没有内置的方法可以获取这些信息?

英文:

In Windows, I can run something like systeminfo | findstr /C:"OS Name to output the Windows full name to the console. I've tried a couple different variations of piping output from one command to the other, but I just get empty strings.

Example

first := exec.Command("systeminfo")
second := exec.Command("findstr /C:'OS Name'")

reader, writer := io.Pipe()

first.Stdout = writer
second.Stdin = reader

var buffer bytes.Buffer
second.Stdout = &buffer

first.Start()
second.Start()
first.Wait()
writer.Close()
second.Wait()

output := buffer.String()

log.Printf("output: %s", output)

`

Are there any built in methods to get this information?

答案1

得分: 1

一种方法是使用golang.org/x/sys/windows/registry包。

一个简单的示例:

package main

import (
	"fmt"
	"golang.org/x/sys/windows/registry"
)

func main() {
	key, _ := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) // 错误被忽略以简洁起见
	defer key.Close()
	productName, _, _ := key.GetStringValue("ProductName") // 错误被忽略以简洁起见
	fmt.Println(productName)
}

这在我的计算机上打印出Windows 8.1 Pro

英文:

One way to do it is to use the golang.org/x/sys/windows/registry package.

A simple example:

package main

import (
	"fmt"
	"golang.org/x/sys/windows/registry"
)

func main() {
	key, _ := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE) // error discarded for brevity
	defer key.Close()
	productName, _, _ := key.GetStringValue("ProductName") // error discarded for brevity
	fmt.Println(productName)
}

This prints Windows 8.1 Pro on my computer.

huangapple
  • 本文由 发表于 2015年10月22日 23:47:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/33285193.html
匿名

发表评论

匿名网友

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

确定