Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

Registry and TreeView

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Shawn B. - 22 Dec 2005 19:39 GMT
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
Michael Höhne - 22 Dec 2005 21:57 GMT
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,
>
[quoted text clipped - 24 lines]
> Thanks,
> Shawn
Jason Newell - 22 Dec 2005 22:14 GMT
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
[quoted text clipped - 35 lines]
>>Thanks,
>>Shawn
Shawn B. - 22 Dec 2005 23:48 GMT
> 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
Michael Höhne - 23 Dec 2005 10:11 GMT
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.
[quoted text clipped - 30 lines]
> Thanks,
> Shawn
Jason Newell - 23 Dec 2005 19:09 GMT
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,
>
[quoted text clipped - 81 lines]
>>Thanks,
>>Shawn
Jason Newell - 23 Dec 2005 19:18 GMT
Sorry, I meant Shawn in my last post.

Jason Newell

> Michael,
>
[quoted text clipped - 96 lines]
>>> Thanks,
>>> Shawn
Shawn B. - 24 Dec 2005 01:50 GMT
Works like a champ.  Now I'll just have to figure out what I'm doing wrong.

Thanks,
Shawn

http://www.zenofdotnet.com

> Michael,
>
[quoted text clipped - 96 lines]
>>>Thanks,
>>>Shawn
Shawn B. - 24 Dec 2005 01:42 GMT
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

> Hi Shawn,
>
[quoted text clipped - 82 lines]
>> Thanks,
>> Shawn

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.