英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论