reduce code

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


Answer this question

reduce code

  • yudkovsky

    This quick (and untested!) snippet should do the trick. Let me know if it works:

    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

    cool, that works, but when i used GetRegistry(1), it doesn't, it works only when i use GetRegistry(0) !

  • Benedikt

    sorry but, nobody answered me, i need the answer urgently please !!

  • Brian Hsi - MSFT

    Both work fine for me:

    GetRegistry(CustomRegistryKey.Run);
    GetRegistry(CustomRegistryKey.RunOnce);

    Perhaps it is possible, and highly likely, that there are no items available in Registry's "RunOnce" key

  • HeatherLK

    Test, ignore asxalalslkds ""guilhermecvm"@discussions.microsoft.com" <"= UTF-8 B Z3VpbGhlcm1lY3Zt ="@discussions.microsoft.com> wrote in message news:d30f8296-725e-4287-819c-eada6654be0f@discussions.microsoft.com... > 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\\CurrentV > ersion\\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\\Sof > tware\\Microsoft\\Windows\\CurrentVersion", "HKLM")); > item1.SubItems.Add(arquivo); > } > hklm.Close(); > #endregion > > > #region HKLM RunOnce > RegistryKey hklm2 = > Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentV > ersion\\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\\So > ftware\\Microsoft\\Windows\\CurrentVersion", "HKLM")); > item2.SubItems.Add(arquivo); > } > hklm2.Close(); > #endregion >
  • athadu

    Test xelaojslals ""guilhermecvm"@discussions.microsoft.com" <"= UTF-8 B Z3VpbGhlcm1lY3Zt ="@discussions.microsoft.com> wrote in message news:d30f8296-725e-4287-819c-eada6654be0f@discussions.microsoft.com... > 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\\CurrentV > ersion\\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\\Sof > tware\\Microsoft\\Windows\\CurrentVersion", "HKLM")); > item1.SubItems.Add(arquivo); > } > hklm.Close(); > #endregion > > > #region HKLM RunOnce > RegistryKey hklm2 = > Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentV > ersion\\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\\So > ftware\\Microsoft\\Windows\\CurrentVersion", "HKLM")); > item2.SubItems.Add(arquivo); > } > hklm2.Close(); > #endregion >
  • FrankLi

    The type 'Microsoft.Win32.RegistryKey' has no constructors defined
  • Sameep

    Hehe, allright a slip of the pen. Try changing:

    RegistryKey hklm = new RegistryKey();

    into:

    RegistryKey hklm = null;

    That'll work.

  • reduce code