【发布时间】:2019-06-25 08:55:37
【问题描述】:
您好,我想知道例如用户的 regedit 键中有多少行数据
计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SystemInformation\ComputerHardwareIds
在我的情况下,我有 10 行 GUID,那么我如何在 c# 中执行此操作?
【问题讨论】:
您好,我想知道例如用户的 regedit 键中有多少行数据
计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SystemInformation\ComputerHardwareIds
在我的情况下,我有 10 行 GUID,那么我如何在 c# 中执行此操作?
【问题讨论】:
来自该链接Registry Data Types(位于注册表中的值名称旁边)
REG_SZ 以 Null 结尾的字符串。它将是 Unicode 或 ANSI 字符串,具体取决于您使用的是 Unicode 还是 ANSI 函数。
REG_MULTI_SZ 由两个空字符终止的以空字符结尾的字符串数组。
Depending on that answer 只是稍微修改了一下以符合您的要求。
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\SystemInformation"))
{
if (key != null)
{
object value = key.GetValue("ComputerHardwareIds");
if (value != null)
{
var computerHardwareIds = (value as string[]); // cast value object to string array, because its type is REG_MULTI_SZ
var lines_num = computerHardwareIds.Length; // then you can get lines number this way
}
}
}
【讨论】: