ruby注册表检查

ris*_*ney 8 ruby windows registry

我有以下片段,我试图用来验证注册表项(操作系统是Windows 2008R2或Win7)

def value_exists?(path,key)
  reg_type = Win32::Registry::KEY_READ 
  Win32::Registry::HKEY_LOCAL_MACHINE.open(path, reg_type) do |reg|
    begin
      regkey = reg[key]
      return true
    rescue
      return false
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

当我执行以下2个命令时,输出是预期的(在我的情况下返回false):

puts(value_exists?("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\",'PendingFileRenameOperations'))
puts(value_exists?("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\",'RebootPending'))
Run Code Online (Sandbox Code Playgroud)

当我表演时

puts(value_exists?("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\",'RebootRequired')) 
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

C:/Ruby187/lib/ruby/1.8/win32/registry.rb:528:in `open': The system cannot find the file specified. (Win32::Registry::Error)
        from C:/Ruby187/lib/ruby/1.8/win32/registry.rb:608:in `open'
        from ./reg2.rb:7:in `value_exists?'
        from ./reg2.rb:21
Run Code Online (Sandbox Code Playgroud)

我真的不明白该怎么做才能做到这一点.我怀疑它可能与x64系统有关,而且无法在正确的位置找到密钥.但我不确定我需要做些什么才能解决这个问题.

感谢您的帮助!

ris*_*ney 8

我想通了 - http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129(v=vs.85).aspx

reg_type = Win32 :: Registry :: KEY_READ | 为0x100

这解决了这个问题!我的猜测是你们没有在x64上测试?

  • 如果这对您有用,请确保将此标记为答案. (2认同)