インストールされているプログラムの情報を返す (Windows)

レジストリを参照して、インストールされているアプリケーションの情報を表示する。
WindowsXP の環境で動作の確認をしました。恐らく他のバージョンでも動くと思います。

require 'win32/registry'

#
# インストールされているプログラムの情報を返す for windows
#
def list_installed_programs
  regkey = %q{SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall}

  list = []

  Win32::Registry::HKEY_LOCAL_MACHINE.open(regkey) {|reg|
    reg.each_key {|subkey, wtime|
      Win32::Registry::HKEY_LOCAL_MACHINE.open("#{regkey}\\#{subkey}") {|subreg|
        begin
          item = {}

          subreg.each_value {|name, type, data|
            item[name.to_sym] = data
          }

          list << item
#         puts "#{subreg['DisplayName']}=#{subreg['UninstallString']}"
        rescue
          # レジスト値が読めなければ無視
        end
      }
    }
  }

  list
end

if __FILE__ == $0
  list = list_installed_programs

  list.each do |i|
    puts "#{i[:DisplayName]} -> #{i[:UninstallString]}"
  end
end