Как в с# получить имя, тип и значение параметра реестра?
Я использовал библиотеку Microsoft.Win32.Registry.dll, методы RegistryKey.getValueNames(), RegistryKey.getValueKind() и RegistryKey.getValue для получения имени, типа и значения параметра соответственно. Вот, что я получил в своем приложении:
Как видите, значения на скриншотах отличаются.
При клике на ключ реестра, я вызываю функцию treeView_registryKeys_AfterSelect, а в ней такой код:
listView_regParData.Items.Clear();
foreach (string valueName in GetRegistryKeyByName(treeView_registryKeys.SelectedNode.Text, regKeysList).GetValueNames())
{
var valueData = new string[] { valueName, GetRegistryKeyByName(treeView_registryKeys.SelectedNode.Text, registries).GetValueKind(valueName).ToString(), (string)GetRegistryKeyByName(treeView_registryKeys.SelectedNode.Text, registries).GetValue(valueName) };
var lvi = new ListViewItem(valueData);
listView_regParData.Items.Add(lvi);
}
GetRegistryKeyByName - это мой собственный метод для получения ключа реестра типа RegistryKey по его string имени (второй параметр - список всех проиндексированных ключей).
Подскажите какие методы нужно использовать. Будет здорово, если вы предоставите код.
UPD: эти методы правильные.
Ответы (1 шт):
Если речь только в названиях типов и отображении их в вашем редакторе, то берем перечисление RegistryValueKind (ссылка), и переписываем как нам надо.
public enum RegistryValueKindNative
{
NONE = -1,
UNKNOWN = 0,
REG_SZ = 1,
REG_EXPAND_SZ = 2,
REG_BINARY = 3,
REG_DWORD = 4,
REG_MULTI_SZ = 7,
REG_QWORD = 11
}
Далее не особо сложно вывести это в строку как душе угодно.
static void Main(string[] args)
{
using (RegistryKey root = Registry.CurrentUser.OpenSubKey("Console"))
{
foreach (string name in root.GetValueNames().OrderBy(x => x))
{
object value = root.GetValue(name);
RegistryValueKind kind = root.GetValueKind(name);
string valueString = (kind == RegistryValueKind.DWord) ? $"0x{((int)value).ToString("X2").ToLower().PadLeft(8,'0')} ({(uint)(int)value})" : value.ToString();
Console.WriteLine($"{name, -25} {(RegistryValueKindNative)kind, -13} {valueString}");
}
}
Console.ReadKey();
}
Вывод в консоль
ColorTable00 REG_DWORD 0x000c0c0c (789516)
ColorTable01 REG_DWORD 0x00da3700 (14300928)
ColorTable02 REG_DWORD 0x000ea113 (958739)
ColorTable03 REG_DWORD 0x00dd963a (14521914)
ColorTable04 REG_DWORD 0x001f0fc5 (2035653)
ColorTable05 REG_DWORD 0x00981788 (9967496)
ColorTable06 REG_DWORD 0x00009cc1 (40129)
ColorTable07 REG_DWORD 0x00cccccc (13421772)
ColorTable08 REG_DWORD 0x00767676 (7763574)
ColorTable09 REG_DWORD 0x00ff783b (16742459)
ColorTable10 REG_DWORD 0x000cc616 (837142)
ColorTable11 REG_DWORD 0x00d6d661 (14079585)
ColorTable12 REG_DWORD 0x005648e7 (5654759)
ColorTable13 REG_DWORD 0x009e00b4 (10354868)
ColorTable14 REG_DWORD 0x00a5f1f9 (10875385)
ColorTable15 REG_DWORD 0x00f2f2f2 (15921906)
CtrlKeyShortcutsDisabled REG_DWORD 0x00000000 (0)
CurrentPage REG_DWORD 0x00000001 (1)
CursorColor REG_DWORD 0xffffffff (4294967295)
CursorSize REG_DWORD 0x00000019 (25)
DefaultBackground REG_DWORD 0xffffffff (4294967295)
DefaultForeground REG_DWORD 0xffffffff (4294967295)
EnableColorSelection REG_DWORD 0x00000000 (0)
ExtendedEditKey REG_DWORD 0x00000001 (1)
ExtendedEditKeyCustom REG_DWORD 0x00000000 (0)
FaceName REG_SZ __DefaultTTFont__
FilterOnPaste REG_DWORD 0x00000001 (1)
FontFamily REG_DWORD 0x00000000 (0)
FontSize REG_DWORD 0x00100000 (1048576)
FontWeight REG_DWORD 0x00000000 (0)
ForceV2 REG_DWORD 0x00000001 (1)
FullScreen REG_DWORD 0x00000000 (0)
HistoryBufferSize REG_DWORD 0x00000032 (50)
HistoryNoDup REG_DWORD 0x00000000 (0)
InsertMode REG_DWORD 0x00000001 (1)
LineSelection REG_DWORD 0x00000001 (1)
LineWrap REG_DWORD 0x00000001 (1)
LoadConIme REG_DWORD 0x00000001 (1)
NumberOfHistoryBuffers REG_DWORD 0x00000004 (4)
PopupColors REG_DWORD 0x000000f5 (245)
QuickEdit REG_DWORD 0x00000001 (1)
ScreenBufferSize REG_DWORD 0x23290078 (589889656)
ScreenColors REG_DWORD 0x00000007 (7)
ScrollScale REG_DWORD 0x00000001 (1)
TerminalScrolling REG_DWORD 0x00000000 (0)
TrimLeadingZeros REG_DWORD 0x00000000 (0)
WindowAlpha REG_DWORD 0x000000ff (255)
WindowSize REG_DWORD 0x001e0078 (1966200)
WordDelimiters REG_DWORD 0x00000000 (0)
Как видите, дело не в том, что C# чего-то не может...


