简体   繁体   English

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

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

It seems that reg delete does not work with keys that have spaces at the end. 似乎reg delete不适用于末尾有空格的键。 I'm trying to delete a key with spaces, for example: 我正在尝试删除带空格的键,例如:

"HKCU\Software\Microsoft\Sample "

Does anyone have any idea on how to delete this, I already use the replace and trim function to convert the registry without space, but unfortunately I can't get it to work. 有没有人对如何删除它有任何想法,我已经使用了replace and trim函数来转换注册表而没有空间,但是不幸的是我无法使它工作。

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

No need to use reg.exe at all. 完全不需要使用reg.exe The WshShell object has a RegDelete method . WshShell对象具有RegDelete方法 The docs say: 文档说:

Specify a key-name by ending strName with a final backslash; 通过以最后的反斜杠结尾strName指定键名; leave it off to specify a value-name. 保留它以指定值名称。

So... 所以...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM