通过VBS删除行尾带空格的注册表项

Delete Registry Key with spaces at the end of line through VBS

似乎 reg delete 不适用于末尾有 space 的键。我正在尝试删除带有 space 的密钥,例如:

"HKCU\Software\Microsoft\Sample "

有没有人知道如何删除它,我已经使用替换和 trim 函数在没有 space 的情况下转换注册表,但不幸的是我无法让它工作。

RegDeleteKey "Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\samplekey ", "TMPUSER"

Function SetKeys(SID)
    RegDeleteKey "Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\sampleykey ", SID
End Function

Sub RegDeleteKey(KeyPath, Mode)
    Dim CMD

    If Mode = "TMPUSER" Then
        KeyPath = "TMPUSER\" & KeyPath
    Else
        KeyPath = Mode & "\" & KeyPath
    End if

    CMD = "REG DELETE " & chr(34) & "HKEY_USERS\" & KeyPath & chr(34) & " /f"

    objshell.Run CMD, 0, True   
End Sub

根本不需要使用reg.exeWshShell object has a RegDelete method。文档说:

Specify a key-name by ending strName with a final backslash; leave it off to specify a value-name.

所以...

Dim Shell: Set Shell = CreateObject("WScript.Shell")


If TryRegDelete("HKCU\Software\Microsoft\Sample \") Then
    WScript.Echo "Success!"
Else
    WScript.Echo "Could not delete key."
End If

Function TryRegDelete(strName)
    On Error Resume Next
    Shell.RegDelete(strName)

    TryRegDelete = Err.Number = 0
    On Error GoTo 0
End Function