There is any way to reduce this code, like not needing to use the same code twice
#region HKLM Run
RegistryKey hklm = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
foreach (string
programas in hklm.GetValueNames())
{
string arquivo = hklm.GetValue(programas).ToString();
ListViewItem item1 = listView1.Items.Add(programas,
imageList1.Images.Count -1);
item1.SubItems.Add(hklm.Name.ToString().Replace("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion",
"HKLM"));
item1.SubItems.Add(arquivo);
}
hklm.Close();
#endregion
#region HKLM RunOnce
RegistryKey
hklm2 = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce");
foreach (string
programas in hklm2.GetValueNames())
{
string arquivo = hklm2.GetValue(programas).ToString();
ListViewItem item2 = listView1.Items.Add(programas,
imageList1.Images.Count - 1);
item2.SubItems.Add(hklm2.Name.ToString().Replace("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion",
"HKLM"));
item2.SubItems.Add(arquivo);
}
hklm2.Close();
#endregion

reduce code
yudkovsky
private enum CustomRegistryKey { Run = 0, RunOnce = 1 };
private void GetRegistry(CustomRegistryKey thisRegistryKey)
{
RegistryKey hklm = new RegistryKey();
switch (thisRegistryKey)
{
case CustomRegistryKey.Run:
hklm = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
break;
case CustomRegistryKey.RunOnce:
hklm = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce");
break;
}
// and the rest can now be the same... etc.
foreach (string programas in hklm.GetValueNames())
{
string arquivo = hklm.GetValue(programas).ToString();
ListViewItem item1 = listView1.Items.Add(programas, imageList1.Images.Count - 1);
item1.SubItems.Add(hklm.Name.ToString().Replace("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion", "HKLM"));
item1.SubItems.Add(arquivo);
}
hklm.Close();
}
praveench2k
Benedikt
Brian Hsi - MSFT
GetRegistry(CustomRegistryKey.Run);
Perhaps it is possible, and highly likely, that there are no items available in Registry's "RunOnce" keyGetRegistry(CustomRegistryKey.RunOnce);
HeatherLK
athadu
FrankLi
Sameep
RegistryKey hklm = new RegistryKey();
into:
RegistryKey hklm = null;
That'll work.