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

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

报告相同问题?

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆