跳轉至內容

Ruby 程式設計/標準庫/Win32::Registry

來自華夏公益教科書,開放的書籍,為開放的世界

原始碼本身也非常有用[1]

如何執行簡單的查詢(值)

[編輯 | 編輯原始碼]

在 win32 登錄檔中,鍵意味著“子鍵”(像資料夾),而值意味著“子項”(像檔案)。

這個示例展示瞭如何檢視值

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

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

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

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

這將導致

CLASSPATH
1
ComSpec
2
FP_NO_HOST_CHECK
1
HOME
...

這個示例展示瞭如何檢視鍵

require 'win32/registry' keyname= "SOFTWARE" # 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

這將導致

... Windows 128883664814843750 Windows 3.1 Migration Status 128783367437500000 WinPcap

預設教程

[編輯 | 編輯原始碼]

這段程式碼在 registry.rb 檔案中給出(由於某種原因,它沒有出現在正常的 rdocs 中)。

<code>
  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
</code>

Win32::Registry.create

[編輯 | 編輯原始碼]
<code>
 Win32::Registry::HKEY_LOCAL_MACHINE.create "software\\abc"
</code>

還要注意,您可以在單個呼叫中進行巢狀建立,例如“software\\classes\\*\\shell\\abc\\subdir\\subdir”。

如何寫入預設值

[編輯 | 編輯原始碼]

透過將 nil 作為名稱傳遞來寫入預設值(在登錄檔編輯器中為“預設”),例如

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

更復雜的示例

[編輯 | 編輯原始碼]

這段程式碼在您右鍵單擊任何檔案時向上下文選單新增一個選項。

name = Win32::Registry::HKEY_LOCAL_MACHINE.create "Software\\classes\\*\\shell\\open_with_arcadia"
name.write_s nil, "play with arcadia"
dir = Win32::Registry::HKEY_LOCAL_MACHINE.create "Software\\classes\\*\\shell\\open_with_arcadia\\command"
dir.write_s nil, %!"#{ruby}" "#{arcadia}" "%1"!
[編輯 | 編輯原始碼]
  1. https://github.com/jruby/jruby/blob/ec40f3789385a64b0286f2771b00c1b4217d409d/lib/ruby/stdlib/win32/registry.rb
華夏公益教科書