471,325 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Registry and TreeView

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
Dec 22 '05 #1
8 4344
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
"Shawn B." <le****@html.com> schrieb im Newsbeitrag
news:uM**************@TK2MSFTNGP09.phx.gbl...
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

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

Jason Newell
Michael Höhne wrote:
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
"Shawn B." <le****@html.com> schrieb im Newsbeitrag
news:uM**************@TK2MSFTNGP09.phx.gbl...
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


Dec 22 '05 #3
> 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
Dec 23 '05 #4
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." <le****@html.com> schrieb im Newsbeitrag
news:OH**************@TK2MSFTNGP14.phx.gbl...
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

Dec 23 '05 #5
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
Michael Höhne wrote:
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." <le****@html.com> schrieb im Newsbeitrag
news:OH**************@TK2MSFTNGP14.phx.gbl...
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


Dec 23 '05 #6
Sorry, I meant Shawn in my last post.

Jason Newell

Jason Newell wrote:
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
Michael Höhne wrote:
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." <le****@html.com> schrieb im Newsbeitrag
news:OH**************@TK2MSFTNGP14.phx.gbl...
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


Dec 23 '05 #7
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" <mi************@nospam.nospam> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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." <le****@html.com> schrieb im Newsbeitrag
news:OH**************@TK2MSFTNGP14.phx.gbl...
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


Dec 24 '05 #8
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" <no****@nospam.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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
Michael Höhne wrote:
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." <le****@html.com> schrieb im Newsbeitrag
news:OH**************@TK2MSFTNGP14.phx.gbl...
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



Dec 24 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

42 posts views Thread by lauren quantrell | last post: by
5 posts views Thread by SoKool | last post: by
2 posts views Thread by Steve Teeples | last post: by
6 posts views Thread by L.M | last post: by
14 posts views Thread by Mr.D | last post: by
8 posts views Thread by Matt MacDonald | last post: by
3 posts views Thread by eSolTec, Inc. 501(c)(3) | last post: by
8 posts views Thread by ross m. greenberg | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.