无法通过golang删除注册表项

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

Not able to delete the registry entry through golang

问题

我用golang编写了一段代码,用于从注册表项中删除一个键,代码如下:

k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\services`, registry.ALL_ACCESS)
if err != nil {
    log.Println("ERROR::<Uninstall> Error in getting the registry values: ", err)
}
defer k.Close()

err = k.DeleteValue("MyKey")
if err != nil {
    log.Println("ERROR::<Uninstall> Error occured while deleting the MyKey Details From the registry ", err)
}

我的输出结果如下:

2016/02/18 13:51:40 ERROR::<Uninstall> Error occured while deleting the MyKey Details From the registry The system cannot find the file specified.

提前感谢。

英文:

I wrote a code in golang to delete a key from the registry entry and my code is as below.

k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\services`, registry.ALL_ACCESS)
		if err != nil {
			log.Println(&quot;ERROR::&lt;Uninstall&gt; Error in getting the registry values: &quot;,err)
		}
		defer k.Close()
		
		err=k.DeleteValue(&quot;MyKey&quot;)
		if err != nil {
			log.Println(&quot;ERROR::&lt;Uninstall&gt; Error occured while deleting the MyKey Details From the registry &quot;,err)
		}

And my output is as below

2016/02/18 13:51:40 ERROR::&lt;Uninstall&gt; Error occured while deleting the MyKey Details From the registry  The system cannot find the file specified.

Thanks in Advance.

答案1

得分: 4

你可以使用registry.DeleteKey来删除注册表中的子键和其对应的值。具体定义如下:

func DeleteKey(k Key, path string) error

其中,DeleteKey函数用于删除键k下路径为path的子键及其值。

示例代码如下:

k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\services`, registry.ALL_ACCESS)

if err != nil {
    log.Fatal(err)
}
defer k.Close()

err := registry.DeleteKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\services`)    
if err != nil {
    log.Fatal(err)
}

你可以参考这里获取更多详细信息。

英文:

You can use registry.DeleteKey which is defined in the following way:

func DeleteKey(k Key, path string) error

> DeleteKey deletes the subkey path of key k and its values.

k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\services`, registry.ALL_ACCESS)

if err != nil {
	log.Fatal(err)
}
defer k.Close()

err := registry.DeleteKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\services`)    
if err != nil {
	log.Fatal(err)
}

答案2

得分: 0

我无法直接运行代码,但我可以为你提供翻译。以下是你提供的代码的翻译:

我无法使这段代码工作所以我改用了 PowerShell这是我的函数

    import("github.com/KnicKnic/go-powershell/pkg/powershell")
    
    ...


    func delreg(service string) {
    	reg := `HKLM:\SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\` + service
    	color.Warn.Println("将删除:" + reg)
    
    	runspace := powershell.CreateRunspaceSimple()
    	defer runspace.Close()
    	GetService := (`Remove-Item -Path "` + reg + `" -Recurse`)
    	results1 := runspace.ExecScript(GetService, false, nil)
    	results1.Close()
    }

希望对你有帮助!如果你有任何其他问题,请随时问我。

英文:

I didn't get anything of this to work, so I used PowerShell instead. Here is my function:

import(&quot;github.com/KnicKnic/go-powershell/pkg/powershell&quot;)

...


func delreg(service string) {
	reg := `HKLM:\SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\` + service
	color.Warn.Println(&quot;Will delete: &quot; + reg)

	runspace := powershell.CreateRunspaceSimple()
	defer runspace.Close()
	GetService := (`Remove-Item -Path &quot;` + reg + `&quot; -Recurse`)
	results1 := runspace.ExecScript(GetService, false, nil)
	results1.Close()
}

huangapple
  • 本文由 发表于 2016年2月18日 16:41:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/35476526.html
匿名

发表评论

匿名网友

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

确定