Registry and TreeView

S

Shawn B.

Greetings,

I'm creating a little program that acts as a glorified Registry explorer.
The TreeView doesn't allow you to show a plus sign unless a node has child
nodes, so I need to determine whether a registry key has subkeys and add a
dummy childnode. The way I'm doing this is to populate the child nodes as
the parent is expanded.

However, there are some serious performance implications. First, when I
pouplate the HKEY_CLASSES_ROOT, it takes 3 seconds to add all the subkeys to
the hive. But if I determine whether those subkeys have subkeys, it takes
49 minutes to populate the HIVE. This is obviously unacceptable. So I'm
left with two choices: 1) figure out a more efficient way to determine and
populate the data, or 2) use C++. Every reg explorer I've ever seen takes
milliseconds to populate the nodes as you expand them, and neither of these
programs are written in C#.

Does anyone here know a better approach? Keep in mind, I'm not populate the
subkeys. I'm only determing whether the subkeys have subkeys (at the level
currently visible) and adding a dummy node so you can get the [+] / [-]
symbol to expand/contract the nodes in the TreeView.

Is it the TreeView that's so slow or the Registry objects in the Win32
namespace? I don't have access to a decent profiler so its hard for me to
tell.


Thanks,
Shawn
 
M

Michael Höhne

There must be something in your code that causes it to take that long. I
wrote a similar application some time ago in C# running on .NET 1.1. I tried
it out right now and it took about 4 seconds to list all keys in HKCR\CLSID,
and I expect that this is the largest one. Note that I'm also doing the
check for subkeys and use the same approach - insert a dummy node if there's
at least one subkey and add the subkeys in the BeforeExpand event.

Could you post the actual code populating the tree view?

Michael
 
J

Jason Newell

Money says he's using Nodes.Add() instead of Nodes.AddRange(). That
would be key in this kind of situation.

Jason Newell

There must be something in your code that causes it to take that long. I
wrote a similar application some time ago in C# running on .NET 1.1. I tried
it out right now and it took about 4 seconds to list all keys in HKCR\CLSID,
and I expect that this is the largest one. Note that I'm also doing the
check for subkeys and use the same approach - insert a dummy node if there's
at least one subkey and add the subkeys in the BeforeExpand event.

Could you post the actual code populating the tree view?

Michael


Greetings,

I'm creating a little program that acts as a glorified Registry explorer.
The TreeView doesn't allow you to show a plus sign unless a node has child
nodes, so I need to determine whether a registry key has subkeys and add a
dummy childnode. The way I'm doing this is to populate the child nodes as
the parent is expanded.

However, there are some serious performance implications. First, when I
pouplate the HKEY_CLASSES_ROOT, it takes 3 seconds to add all the subkeys
to the hive. But if I determine whether those subkeys have subkeys, it
takes 49 minutes to populate the HIVE. This is obviously unacceptable.
So I'm left with two choices: 1) figure out a more efficient way to
determine and populate the data, or 2) use C++. Every reg explorer I've
ever seen takes milliseconds to populate the nodes as you expand them, and
neither of these programs are written in C#.

Does anyone here know a better approach? Keep in mind, I'm not populate
the subkeys. I'm only determing whether the subkeys have subkeys (at the
level currently visible) and adding a dummy node so you can get the [+] /
[-] symbol to expand/contract the nodes in the TreeView.

Is it the TreeView that's so slow or the Registry objects in the Win32
namespace? I don't have access to a decent profiler so its hard for me to
tell.


Thanks,
Shawn
 
S

Shawn B.

Money says he's using Nodes.Add() instead of Nodes.AddRange(). That would
be key in this kind of situation.

Yes I am. I'll have to redo the code for AddRange() if its really that much
more efficient. My code is very simple, the relevant part is:

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();

tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

regKey.Close();
node.Nodes.Add(child);
}

regKey.Close();

tvwRegistryKeys.EndUpdate();
}


Thanks,
Shawn
 
M

Michael Höhne

Hi Shawn,

I agree with Jason that AddRange is preferable, but your problem clearly is
different. In your foreach loop you do a call to RegistryKey.OpenSubKey but
don't store the result in any variable:
foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);

Now that means that the next block will always operate on the same registry
key:
if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

If this registry key is HKEY_CLASSES_ROOT, then regKey.SubKeyCount will take
an enormous amount of time to complete and you're calling it over and over.

Try the following and I'm quite sure that your tree is populated in a few
seconds. After you've verified that, you should go for AddRange to improve
it furhter.

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();
tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
RegistryKey regSubkey = regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regSubkey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

node.Nodes.Add(child);
regSubkey.Close();
}

regKey.Close();
}



Michael


Shawn B. said:
Money says he's using Nodes.Add() instead of Nodes.AddRange(). That
would be key in this kind of situation.

Yes I am. I'll have to redo the code for AddRange() if its really that
much more efficient. My code is very simple, the relevant part is:

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();

tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

regKey.Close();
node.Nodes.Add(child);
}

regKey.Close();

tvwRegistryKeys.EndUpdate();
}


Thanks,
Shawn
 
J

Jason Newell

Michael,

If you're interested, I threw together a little C# 2005 regedit program
to show you what I'm talking about. I compared it to the Windows
regedit and it's slightly slower but nothing like what you described. I
don't know that you can get much faster with .NET. Let me know if you
have any questions.

You can download the source from my website.

www.jasonnewell.net/public/Examples/Regedit.zip

Jason Newell

Hi Shawn,

I agree with Jason that AddRange is preferable, but your problem clearly is
different. In your foreach loop you do a call to RegistryKey.OpenSubKey but
don't store the result in any variable:

foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);


Now that means that the next block will always operate on the same registry
key:

if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}


If this registry key is HKEY_CLASSES_ROOT, then regKey.SubKeyCount will take
an enormous amount of time to complete and you're calling it over and over.

Try the following and I'm quite sure that your tree is populated in a few
seconds. After you've verified that, you should go for AddRange to improve
it furhter.

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();
tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
RegistryKey regSubkey = regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regSubkey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

node.Nodes.Add(child);
regSubkey.Close();
}

regKey.Close();
}



Michael


Money says he's using Nodes.Add() instead of Nodes.AddRange(). That
would be key in this kind of situation.

Yes I am. I'll have to redo the code for AddRange() if its really that
much more efficient. My code is very simple, the relevant part is:

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();

tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

regKey.Close();
node.Nodes.Add(child);
}

regKey.Close();

tvwRegistryKeys.EndUpdate();
}


Thanks,
Shawn
 
J

Jason Newell

Sorry, I meant Shawn in my last post.

Jason Newell

Jason said:
Michael,

If you're interested, I threw together a little C# 2005 regedit program
to show you what I'm talking about. I compared it to the Windows
regedit and it's slightly slower but nothing like what you described. I
don't know that you can get much faster with .NET. Let me know if you
have any questions.

You can download the source from my website.

www.jasonnewell.net/public/Examples/Regedit.zip

Jason Newell

Hi Shawn,

I agree with Jason that AddRange is preferable, but your problem
clearly is different. In your foreach loop you do a call to
RegistryKey.OpenSubKey but don't store the result in any variable:

foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);



Now that means that the next block will always operate on the same
registry key:

if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}



If this registry key is HKEY_CLASSES_ROOT, then regKey.SubKeyCount
will take an enormous amount of time to complete and you're calling it
over and over.

Try the following and I'm quite sure that your tree is populated in a
few seconds. After you've verified that, you should go for AddRange to
improve it furhter.

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();
tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
RegistryKey regSubkey = regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regSubkey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

node.Nodes.Add(child);
regSubkey.Close();
}

regKey.Close();
}



Michael


Money says he's using Nodes.Add() instead of Nodes.AddRange(). That
would be key in this kind of situation.


Yes I am. I'll have to redo the code for AddRange() if its really
that much more efficient. My code is very simple, the relevant part is:

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();

tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

regKey.Close();
node.Nodes.Add(child);
}

regKey.Close();

tvwRegistryKeys.EndUpdate();
}


Thanks,
Shawn
 
S

Shawn B.

Everytime I run it I keept getting "Object reference not set to an instance
of an object when it tries to return an instance by OpenSubKey(...); The
key path looks valid, I'm not sure what to do.

Thanks,
Shawn


Michael Höhne said:
Hi Shawn,

I agree with Jason that AddRange is preferable, but your problem clearly
is different. In your foreach loop you do a call to RegistryKey.OpenSubKey
but don't store the result in any variable:
foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);

Now that means that the next block will always operate on the same
registry key:
if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

If this registry key is HKEY_CLASSES_ROOT, then regKey.SubKeyCount will
take an enormous amount of time to complete and you're calling it over and
over.

Try the following and I'm quite sure that your tree is populated in a few
seconds. After you've verified that, you should go for AddRange to improve
it furhter.

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();
tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
RegistryKey regSubkey = regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regSubkey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

node.Nodes.Add(child);
regSubkey.Close();
}

regKey.Close();
}



Michael


Shawn B. said:
Money says he's using Nodes.Add() instead of Nodes.AddRange(). That
would be key in this kind of situation.

Yes I am. I'll have to redo the code for AddRange() if its really that
much more efficient. My code is very simple, the relevant part is:

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();

tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

regKey.Close();
node.Nodes.Add(child);
}

regKey.Close();

tvwRegistryKeys.EndUpdate();
}


Thanks,
Shawn
 
S

Shawn B.

Works like a champ. Now I'll just have to figure out what I'm doing wrong.


Thanks,
Shawn

http://www.zenofdotnet.com



Jason Newell said:
Michael,

If you're interested, I threw together a little C# 2005 regedit program to
show you what I'm talking about. I compared it to the Windows regedit and
it's slightly slower but nothing like what you described. I don't know
that you can get much faster with .NET. Let me know if you have any
questions.

You can download the source from my website.

www.jasonnewell.net/public/Examples/Regedit.zip

Jason Newell

Hi Shawn,

I agree with Jason that AddRange is preferable, but your problem clearly
is different. In your foreach loop you do a call to
RegistryKey.OpenSubKey but don't store the result in any variable:

foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);


Now that means that the next block will always operate on the same
registry key:

if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}


If this registry key is HKEY_CLASSES_ROOT, then regKey.SubKeyCount will
take an enormous amount of time to complete and you're calling it over
and over.

Try the following and I'm quite sure that your tree is populated in a few
seconds. After you've verified that, you should go for AddRange to
improve it furhter.

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();
tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
RegistryKey regSubkey = regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regSubkey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

node.Nodes.Add(child);
regSubkey.Close();
}

regKey.Close();
}



Michael


Money says he's using Nodes.Add() instead of Nodes.AddRange(). That
would be key in this kind of situation.

Yes I am. I'll have to redo the code for AddRange() if its really that
much more efficient. My code is very simple, the relevant part is:

if (canContinue == true)
{
string[] names = regKey.GetSubKeyNames();

tvwRegistryKeys.BeginUpdate();

foreach (string key in names)
{
regKey.OpenSubKey(path + @"\" + key);
TreeNode child = new TreeNode(key);

if (regKey.SubKeyCount > 0)
{
TreeNode childNode = new TreeNode("*");
child.Nodes.Add(childNode);
}

regKey.Close();
node.Nodes.Add(child);
}

regKey.Close();

tvwRegistryKeys.EndUpdate();
}


Thanks,
Shawn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top