英文:
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("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)
}
And my output is as below
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.
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("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("Will delete: " + reg)
runspace := powershell.CreateRunspaceSimple()
defer runspace.Close()
GetService := (`Remove-Item -Path "` + reg + `" -Recurse`)
results1 := runspace.ExecScript(GetService, false, nil)
results1.Close()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论