类 Win32::Registry
属性
Disposition 值 (REG_CREATED_NEW_KEY 或 REG_OPENED_EXISTING_KEY)。
返回键句柄值。
与 Registry.open
或 Registry.create
方法的子键值相同。
Win32::Registry
父键对象,如果为预定义键则为 nil。
公共类方法
— Registry.create
(key, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED)
— Registry.create
(key, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED) { |reg| … }
在 key 下创建或打开注册表键 subkey。您可以使用预定义键 HKEY_*(参见 Constants
)
如果 subkey 已经存在,则打开 key 并且 Registry#created?
方法将返回 false。
如果提供了代码块,则 key 会自动关闭。
# File ext/win32/lib/win32/registry.rb, line 457 def self.create(hkey, subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED) newkey, disp = API.CreateKey(hkey.hkey, subkey, opt, desired) obj = new(newkey, hkey, subkey, disp) if block_given? begin yield obj ensure obj.close end else obj end end
将 %w+% 替换为 % 符号之间包含的环境值。此方法用于 REG_EXPAND_SZ。
有关详细信息,请参见 expandEnvironmentStrings Win32 API。
# File ext/win32/lib/win32/registry.rb, line 373 def self.expand_environ(str) str.gsub(Regexp.compile("%([^%]+)%".encode(str.encoding))) { v = $1.encode(LOCALE) (e = ENV[v] || ENV[v.upcase]; e.encode(str.encoding) if e) || $& } end
初始化
# File ext/win32/lib/win32/registry.rb, line 479 def initialize(hkey, parent, keyname, disposition) @hkey = hkey @parent = parent @keyname = keyname @disposition = disposition @hkeyfinal = [ hkey ] ObjectSpace.define_finalizer self, @@final.call(@hkeyfinal) end
— Registry.open
(key, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED)
— Registry.open
(key, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED) { |reg| … }
打开注册表键 subkey 在 key 下。key 是 Win32::Registry
父键对象。您可以使用预定义的键 HKEY_*(参见 Constants
)desired 和 opt 是访问掩码和键选项。有关详细信息,请参见 MSDN。如果给出了块,则键将自动关闭。
# File ext/win32/lib/win32/registry.rb, line 429 def self.open(hkey, subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED) subkey = subkey.chomp('\\') newkey = API.OpenKey(hkey.hkey, subkey, opt, desired) obj = new(newkey, hkey, subkey, REG_OPENED_EXISTING_KEY) if block_given? begin yield obj ensure obj.close end else obj end end
将注册表类型值转换为可读字符串。
# File ext/win32/lib/win32/registry.rb, line 395 def self.type2name(type) @@type2name[type] || type.to_s end
将 64 位 FILETIME 整数转换为 Time
对象。
# File ext/win32/lib/win32/registry.rb, line 402 def self.wtime2time(wtime) Time.at((wtime - 116444736000000000) / 10000000) end
公共实例方法
读取名为 name 的注册表值并返回其值数据。值的类与 read
方法返回的类相同。
如果值类型为 REG_EXPAND_SZ,则返回已替换环境变量的值数据。如果值类型既不是 REG_SZ、REG_MULTI_SZ、REG_DWORD、REG_DWORD_BIG_ENDIAN,也不是 REG_QWORD,则会引发 TypeError
。
rtype 的含义与 read
方法相同。
# File ext/win32/lib/win32/registry.rb, line 679 def [](name, *rtype) type, data = read(name, *rtype) case type when REG_SZ, REG_DWORD, REG_QWORD, REG_MULTI_SZ data when REG_EXPAND_SZ Registry.expand_environ(data) else raise TypeError, "Type #{Registry.type2name(type)} is not supported." end end
将值写入名为 name 的注册表值。
如果指定了 wtype,则值类型为它。否则,值类型取决于值的类::Integer
REG_DWORD
: 字符串
REG_SZ
: 数组
REG_MULTI_SZ
# File ext/win32/lib/win32/registry.rb, line 774 def []=(name, rtype, value = nil) if value write name, rtype, value else case value = rtype when Integer write name, REG_DWORD, value when String write name, REG_SZ, value when Array write name, REG_MULTI_SZ, value else raise TypeError, "Unexpected type #{value.class}" end end value end
不允许序列化
# File ext/win32/lib/win32/registry.rb, line 534 def _dump(depth) raise TypeError, "can't dump Win32::Registry" end
关闭键。
关闭后,大多数方法会引发错误。
# File ext/win32/lib/win32/registry.rb, line 557 def close API.CloseKey(@hkey) @hkey = @parent = @keyname = nil @hkeyfinal[0] = nil end
与 Win32::Registry.create
(self, subkey, desired, opt) 相同
# File ext/win32/lib/win32/registry.rb, line 548 def create(subkey, desired = KEY_ALL_ACCESS, opt = REG_OPTION_RESERVED, &blk) self.class.create(self, subkey, desired, opt, &blk) end
返回键是否已创建(**新创建的**)。(参见 Registry.create
) - 基本上,您调用 create,然后当您在返回的实例上调用 created? 时,它会告诉您是否成功。
# File ext/win32/lib/win32/registry.rb, line 504 def created? @disposition == REG_CREATED_NEW_KEY end
删除名为 name 的子键及其所有值。
如果 recursive 为 false,则子键不能有子键。否则,此方法将递归删除所有子键和值。
# File ext/win32/lib/win32/registry.rb, line 837 def delete_key(name, recursive = false) if recursive open(name, KEY_ALL_ACCESS) do |reg| reg.keys.each do |key| begin reg.delete_key(key, true) rescue Error # end end end API.DeleteKey(@hkey, name) else begin API.EnumKey @hkey, 0 rescue Error return API.DeleteKey(@hkey, name) end raise Error.new(5) ## ERROR_ACCESS_DENIED end end
删除名为 name 的注册表值。我们不能删除“默认”值。
# File ext/win32/lib/win32/registry.rb, line 826 def delete_value(name) API.DeleteValue(@hkey, name) end
枚举子键。
subkey 是 String
,包含子键的名称。wtime 是最后写入时间,以 FILETIME(64 位整数)表示。(参见 Registry.wtime2time
)
# File ext/win32/lib/win32/registry.rb, line 603 def each_key index = 0 while true begin subkey, wtime = API.EnumKey(@hkey, index) rescue Error break end subkey = export_string(subkey) yield subkey, wtime index += 1 end index end
枚举值。
# File ext/win32/lib/win32/registry.rb, line 566 def each_value index = 0 while true begin subkey = API.EnumValue(@hkey, index) rescue Error break end subkey = export_string(subkey) begin type, data = read(subkey) rescue Error else yield subkey, type, data end index += 1 end index end
将所有属性写入注册表文件。
# File ext/win32/lib/win32/registry.rb, line 862 def flush API.FlushKey @hkey end
将键信息作为 Array
返回,包含以下内容::num_keys
The number of subkeys.
:max_key_length
Maximum length of name of subkeys.
:num_values
The number of values.
:max_value_name_length
Maximum length of name of values.
:max_value_length
Maximum length of value of values.
:descriptor_length
Length of security descriptor.
:wtime
Last write time as FILETIME(64-bit integer)
有关详细信息,请参阅 RegQueryInfoKey Win32
API
。
# File ext/win32/lib/win32/registry.rb, line 885 def info API.QueryInfoKey(@hkey) end
# File ext/win32/lib/win32/registry.rb, line 527 def inspect "\#<Win32::Registry key=#{name.inspect}>" end
将键作为数组返回。
# File ext/win32/lib/win32/registry.rb, line 621 def keys keys_ary = [] each_key { |key,| keys_ary << key } keys_ary end
键的完整路径,例如“HKEY_CURRENT_USERSOFTWAREfoobar”。
# File ext/win32/lib/win32/registry.rb, line 518 def name parent = self name = @keyname while parent = parent.parent name = parent.keyname + '\\' + name end name end
与 Win32::Registry.open
(self, subkey, desired, opt) 相同。
# File ext/win32/lib/win32/registry.rb, line 541 def open(subkey, desired = KEY_READ, opt = REG_OPTION_RESERVED, &blk) self.class.open(self, subkey, desired, opt, &blk) end
返回键是否未关闭。
# File ext/win32/lib/win32/registry.rb, line 511 def open? !@hkey.nil? end
读取名为 name 的注册表值,并返回 [ type, data ] 数组。当 name 为 nil 时,将读取“默认”值。type 是值类型。(请参阅 Win32::Registry::Constants
模块)data 是值数据,其类为::REG_SZ, REG_EXPAND_SZ
String
:REG_MULTI_SZ
Array of String
:REG_DWORD, REG_DWORD_BIG_ENDIAN, REG_QWORD
Integer
:REG_BINARY, REG_NONE
String (contains binary data)
当指定 rtype 时,值类型必须包含在 rtype 数组中,否则将引发 TypeError
。
# File ext/win32/lib/win32/registry.rb, line 643 def read(name, *rtype) type, data = API.QueryValue(@hkey, name) unless rtype.empty? or rtype.include?(type) raise TypeError, "Type mismatch (expect [#{ rtype.map{|t|Registry.type2name(t)}.join(', ')}] but #{ Registry.type2name(type)} present)" end case type when REG_SZ, REG_EXPAND_SZ [ type, data.encode(name.encoding, WCHAR).chop ] when REG_MULTI_SZ [ type, data.encode(name.encoding, WCHAR).split(/\0/) ] when REG_BINARY, REG_NONE [ type, data ] when REG_DWORD [ type, API.unpackdw(data) ] when REG_DWORD_BIG_ENDIAN [ type, data.unpack('N')[0] ] when REG_QWORD [ type, API.unpackqw(data) ] else raise TypeError, "Type #{Registry.type2name(type)} is not supported." end end
读取名为 name 的 REG_SZ 或 REG_EXPAND_SZ 注册表值。
如果值类型为 REG_EXPAND_SZ,则会替换环境变量。除非值类型为 REG_SZ 或 REG_EXPAND_SZ,否则会引发 TypeError
。
# File ext/win32/lib/win32/registry.rb, line 705 def read_s_expand(name) type, data = read(name, REG_SZ, REG_EXPAND_SZ) if type == REG_EXPAND_SZ Registry.expand_environ(data) else data end end
将值作为数组返回。
# File ext/win32/lib/win32/registry.rb, line 590 def values vals_ary = [] each_value { |*, val| vals_ary << val } vals_ary end
将数据写入名为 name 的注册表值。当 name 为 nil 时,写入“默认”值。
type 是类型值。(参见 Registry::Constants
模块)data 的 Class
必须与 read
方法返回的类型相同。
# File ext/win32/lib/win32/registry.rb, line 742 def write(name, type, data) case type when REG_SZ, REG_EXPAND_SZ data = data.encode(WCHAR) << WCHAR_NUL when REG_MULTI_SZ data = data.to_a.map {|s| s.encode(WCHAR)}.join(WCHAR_NUL) << WCHAR_NUL when REG_BINARY, REG_NONE data = data.to_s when REG_DWORD data = API.packdw(data.to_i) when REG_DWORD_BIG_ENDIAN data = [data.to_i].pack('N') when REG_QWORD data = API.packqw(data.to_i) else raise TypeError, "Unsupported type #{Registry.type2name(type)}" end API.SetValue(@hkey, name, type, data, data.bytesize) end