reduce code  
Author Message
guilhermecvm94558





PostPosted: Visual C# General, reduce code Top

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


Visual C#7  
 
 
Julien





PostPosted: Visual C# General, reduce code Top

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();

}



 
 
guilhermecvm





PostPosted: Visual C# General, reduce code Top

The type 'Microsoft.Win32.RegistryKey' has no constructors defined
 
 
Julien





PostPosted: Visual C# General, reduce code Top

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

RegistryKey hklm = new RegistryKey();

into:

RegistryKey hklm = null;

That'll work.

 
 
guilhermecvm





PostPosted: Visual C# General, reduce code Top

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

 
 
guilhermecvm





PostPosted: Visual C# General, reduce code Top

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

 
 
Julien





PostPosted: Visual C# General, reduce code Top

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

 
 
MVP User





PostPosted: Visual C# General, reduce code Top


 
 
MVP User





PostPosted: Visual C# General, reduce code Top