NSIS - 检查是否存在注册表项值

时间:2015-01-08 12:11:01

标签: nsis

如果存在注册表值,我需要检查。我怎么能这样做?

我的第一个方法:

ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:"
        ${IF} $0 == ""
              MESSAGEBOX MB_OK "NUL exists"
        ${ELSE}
               WriteRegStr HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:" ""
        ${ENDIF}

但是当价值不存在时,这也有效。我想,因为"不存在"和空字符串的处理方式相同。

使用Registry.nsh我这样做了:

${registry::Read} "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:" $var1 $var2

        ${IF} $var2 == "REG_SZ"

但是我收到错误,因为registry.nsh中的Pop $ {_ STRING}不起作用。

欢迎提供帮助和建议!

1 个答案:

答案 0 :(得分:11)

您应该在阅读后检查错误标志:

ClearErrors
ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:"
${If} ${Errors}
  MessageBox MB_OK "Value not found"
${Else}
  ${IF} $0 == ""
              MESSAGEBOX MB_OK "NUL exists and it's empty"
        ${ELSE}
               WriteRegStr HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports" "NUL:" ""
        ${ENDIF}
${EndIf}

另外,在尝试阅读之前,您可能会对EnumRegValue感兴趣。