Ruby Programming/Standard Library/Win32::Registry

The source code itself is also pretty useful.

= How to do a simple query (values) =

With the win32 registry, keys mean &ldquo;subkey&rdquo; (like a folder), and values mean &ldquo;subentry&rdquo; (like file).

This example shows how to look at values:

require 'win32/registry' keyname= 'SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment'

access = Win32::Registry::KEY_ALL_ACCESS
 * 1) KEY_ALL_ACCESS enables you to write and deleted.
 * 2) the default access is KEY_READ if you specify nothing

Win32::Registry::HKEY_LOCAL_MACHINE.open(keyname, access) do |reg| # each is the same as each_value, because #each_key actually means # "each child folder" so #each doesn't list any child folders... # use #keys for that... reg.each{|name, value| puts name, value} end

or

a = Win32::Registry::HKEY_LOCAL_MACHINE.open \ "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", Win32::Registry::KEY_READ a.each{|n, v| p n, v} a.close

which results in:

CLASSPATH 1 ComSpec 2 FP_NO_HOST_CHECK 1 HOME ...

and this example shows you how to look at keys: require 'win32/registry' keyname= &quot;SOFTWARE&quot; # this isn't actually case sensitive, but hey access = Win32::Registry::KEY_ALL_ACCESS Win32::Registry::HKEY_LOCAL_MACHINE.open(keyname, access) do |reg|; reg.each_key{|k, v| puts k, v} end which results in: ... Windows 128883664814843750 Windows 3.1 Migration Status 128783367437500000 WinPcap

= Default tutorial =

This code is given in the registry.rb file (doesn&rsquo;t show up in the normal rdocs for some reason) &lt;code&gt; Win32::Registry::HKEY_CURRENT_USER.open('SOFTWARE\foo') do |reg| value = reg['foo']                              # read a value value = reg['foo', Win32::Registry::REG_SZ]     # read a value with type type, value = reg.read('foo')                   # read a value reg['foo'] = 'bar'                              # write a value reg['foo', Win32::Registry::REG_SZ] = 'bar'     # write a value with type reg.write('foo', Win32::Registry::REG_SZ, 'bar') # write a value reg.each_value { |name, type, data| ... }       # Enumerate values reg.each_key { |key, wtime| ... }               # Enumerate subkeys reg.delete_value(name)                        # Delete a value reg.delete_key(name)                          # Delete a subkey reg.delete_key(name, true)                    # Delete a subkey recursively end &lt;/code&gt; = Win32::Registry.create =

&lt;code&gt; Win32::Registry::HKEY_LOCAL_MACHINE.create &quot;software\\abc&quot; &lt;/code&gt; Note also that you can do nested creates in a single call, like &ldquo;software\\classes\\*\\shell\\abc\\subdir\\subdir&rdquo;

= How to write default values: =

Write default values ("Default" in regedit) by passing nil as the name, ex:

a.write_s nil, "a default string" # and read it back a.read_s nil

More complex example
This code adds an option to the context menu with you right click on any file.

name = Win32::Registry::HKEY_LOCAL_MACHINE.create &quot;Software\\classes\\*\\shell\\open_with_arcadia&quot; name.write_s nil, &quot;play with arcadia&quot; dir = Win32::Registry::HKEY_LOCAL_MACHINE.create &quot;Software\\classes\\*\\shell\\open_with_arcadia\\command&quot; dir.write_s nil, %!&quot;#{ruby}&quot; &quot;#{arcadia}&quot; &quot;%1&quot;!

Related links

 * http://rubyforge.org/snippet/detail.php?type=snippet&amp;id=13