Create a registry key/string with Go

ぐ巨炮叔叔 提交于 2020-01-24 14:05:07

问题


I was following this document to create a key/string in windows registry with this code snippet:

package main

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

func main() {

    k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Audio`, registry.QUERY_VALUE)
    if err != nil {
        log.Fatal(err)
    }
    k.SetStringValue("xyz", "blahblah")
    err = k.Close()
    if err != nil {
        log.Fatal(err)
    }
}

but nothing happens, without any errors. Edit (clarification): It doesn't work, in any circumstances.


回答1:


You are opening the key with only the QUERY_VALUE permission, but you also need SET_VALUE in order to successfully call SetStringValue.

You should also be checking the return value on SetStringValue, which would have likely informed you what the problem was.

k, err := registry.OpenKey(registry.CURRENT_USER, `Software\Microsoft\Windows\CurrentVersion\Audio`, registry.QUERY_VALUE|registry.SET_VALUE)
if err != nil {
    log.Fatal(err)
}
if err := k.SetStringValue("xyz", "blahblah"); err != nil {
    log.Fatal(err)
}
if err := k.Close(); err != nil {
    log.Fatal(err)
}


来源:https://stackoverflow.com/questions/46542609/create-a-registry-key-string-with-go

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!