Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Win32注册表:系统在查找不带';不存在_Ruby_Registry - Fatal编程技术网

Ruby Win32注册表:系统在查找不带';不存在

Ruby Win32注册表:系统在查找不带';不存在,ruby,registry,Ruby,Registry,我正在编写一个ruby函数,从给定的DisplayName获取卸载字符串。我的函数只有在我给它一个要查找的现有DisplayName时才能成功运行,当我给它一个不存在的东西时它就会崩溃。我不确定在什么条件下检查哪个变量(nil?empty?),这样我就可以向脚本抛出一个异常,打印一条消息“notfound”,而不是崩溃 require 'win32/registry' def get_uninstallstring(app_name) paths = [ "Software\\Micro

我正在编写一个ruby函数,从给定的DisplayName获取卸载字符串。我的函数只有在我给它一个要查找的现有DisplayName时才能成功运行,当我给它一个不存在的东西时它就会崩溃。我不确定在什么条件下检查哪个变量(nil?empty?),这样我就可以向脚本抛出一个异常,打印一条消息“notfound”,而不是崩溃

require 'win32/registry'

def get_uninstallstring(app_name)

  paths = [ "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
            "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
            "Software\\Wow6464Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall"  ]
  access_type = Win32::Registry::KEY_ALL_ACCESS

  paths.each do |path|         # This is line 9
    Win32::Registry::HKEY_LOCAL_MACHINE.open(path, access_type) do |reg|
      reg.each_key do |key|
        k = reg.open(key) 
        app = k['DisplayName'] rescue nil
        if app == app_name
          str = k['UninstallString'] rescue nil
          return str
        end
      end
    end
  end
  return false
end

puts get_uninstallstring("ABC")    # This is line 24
以下是错误输出:

C:/ruby/1.9.1/win32/registry.rb:385:in `open': The system cannot find the file specified. (Win32::Registry::Error)
        from C:/heliopolis/opscode/chef/embedded/lib/ruby/1.9.1/win32/registry.rb:496:in `open'
        from test.rb:10:in `block in get_uninstallstring'
        from test.rb:9:in `each'
        from test.rb:9:in `get_uninstallstring'
        from test.rb:24:in `<main>'
C:/ruby/1.9.1/win32/registry.rb:385:在“打开”中:系统找不到指定的文件。(Win32::注册表::错误)
来自C:/heliopolis/opscode/chef/embedded/lib/ruby/1.9.1/win32/registry.rb:496:in'open'
from test.rb:10:in'block in get_uninstallstring'
来自测试。rb:9:in'each'
来自test.rb:9:in'get\u uninstallstring'
来自测试。rb:24:in`'

所以我自己找到了一个解决方案。我必须编写另一个函数来检查键是否存在于给定的路径中,然后才能实际获取其值

def key_exists?(path)
  begin
    Win32::Registry::HKEY_LOCAL_MACHINE.open(path, ::Win32::Registry::KEY_READ)
    return true
  rescue
    return false
  end
end
下面是修改后的get函数:

  paths.each do |path| 
    if key_exists?(path)
      Win32::Registry::HKEY_LOCAL_MACHINE.open(path, access_type) do |reg|
        reg.each_key do |key|
          k = reg.open(key)
          app = k['DisplayName'] rescue nil
          if app == app_name
        return k['UninstallString'] rescue nil
          end
        end
      end
    else
      return false
    end
  end