doue9730 2017-10-03 10:37 采纳率: 100%
浏览 321
已采纳

使用Go创建注册表项/字符串

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条回答 默认 最新

  • doushichi3678 2017-10-03 11:53
    关注

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言爬虫的时候元素和园代码不一样怎么解决呀
  • ¥15 VS2022多项目启动有问题
  • ¥15 SQL删除添加数据后序号不连续问题。
  • ¥15 首次运行OmniEvent运行报错
  • ¥15 有没有人知道这个问题怎么解决
  • ¥15 comsol电力电缆载流量仿真
  • ¥15 webSocket可以接TCP socket接口吗
  • ¥60 mpi并行出错,CFD++计算
  • ¥15 c#:vsto,powerpoint的外接程序中换主题颜色
  • ¥15 状态机/汽车转向灯/Sateflow