NSIS - check if registry key value exists

拟墨画扇 提交于 2019-12-07 01:42:17

问题


I need to check, if a registry value exists. How can I do that?

My first approach:

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}

But this also works, when the value doesn’t exist. I guess, because "doesn’t exist" and empty string are handled the same way.

With Registry.nsh I did it like this:

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

        ${IF} $var2 == "REG_SZ"

But I get an error, because the Pop ${_STRING} in the registry.nsh doesn’t work.

Help and suggestions welcome!


回答1:


You should check the error flag after reading:

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}

Also, you may be interested in EnumRegValue before trying to read it.



来源:https://stackoverflow.com/questions/27839860/nsis-check-if-registry-key-value-exists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!