Click here to Skip to main content
11,492,523 members (63,004 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET registry
Hello, All.

Can someone help me to speed up the loading of application.
I'm creating a program like a Registry Editor but for viewing purposes only.
The program loads too slow because of my Try-catch for the security issues.

Imports Microsoft.Win32
Imports System.Security
 
Public Class Form1
 
Private Function CreateNodes(ByVal vParentNode As TreeNode, ByVal vRegKey As RegistryKey) As TreeNode
For Each vSubKeyName As String In vRegKey.GetSubKeyNames()
Try
' Open subkey and create a childnode with subkeys name on it '
' Then create childnodes for childnode ' 

 
Dim vSubKey As RegistryKey = vRegKey.OpenSubKey(vSubKeyName, RegistryKeyPermissionCheck.Default, Security.AccessControl.RegistryRights.ReadKey)
Dim vChildNode As New TreeNode(vSubKeyName)
'Dim vChildNode As New TreeNode(vSubKeyName.Name)
vChildNode = CreateNodes(vChildNode, vSubKey)
vParentNode.Nodes.Add(vChildNode)
' vParentNode.Nodes.Add(vSubKeyName)

Catch ex As SecurityException
 
' Lots of security exceptions will be thrown if user is not admin or doesnt have access for whole registry ' 
Catch ex As Exception
 

End Try
Next
Return vParentNode
 
End Function
 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Get registrykey for LocalMachine '

Dim vRegKeyLocalMachine As RegistryKey = Registry.LocalMachine
Dim vRegKeyClassesRoot As RegistryKey = Registry.ClassesRoot
 
' Create TreeNode and get its child nodes in CreateNodes method '

Dim vParentNode As New TreeNode(vRegKeyLocalMachine.Name)
Dim vParentNode2 As New TreeNode(vRegKeyClassesRoot.Name)
 
vParentNode = CreateNodes(vParentNode, vRegKeyLocalMachine)
vParentNode2 = CreateNodes(vParentNode2, vRegKeyClassesRoot)
' Show the nodes on treeview '

Dim root As TreeNode
root = New TreeNode(My.Computer.Name)
 
TreeView1.Nodes.Add(root)
TreeView1.Nodes.Add(vParentNode2)
TreeView1.Nodes.Add(vParentNode)
 
End Sub
End Class

or do you have code bank there to use or borrow?

Regards,
Jeff
Posted 24-Jan-13 5:27am
aljeff169
Edited 24-Jan-13 7:25am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You could try moving the try..catch code outside of the for..next loop in your CreateNodes code
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The first solution is correct, you should move the try..catch outside the loop to correct the performance problems.

However, you can also use the RegistryRights enumeration in the RegistrySecurity class that is part of System.Security.AccessControl to test to see if your application is running under an account with sufficient permissions to do what you are wanting to do. There is also a GetAccessControl method on the RegistyKey class that allows you to get the user rights associated with a specific key.

Checking the permissions first and failing gracefully is a much better approach that assuming everything will work then trying to catch the possibilities in a catch block.
  Permalink  
Comments
aljeff at 24-Jan-13 11:53am
   
If I remove the try catch outside the loop it will return an error because of security issues.

Im getting confused.
Jason Gleim at 24-Jan-13 11:58am
   
If you write the function CreateNodes as

Try
For Each vSubKeyName As String In vRegKey.GetSubKeyNames()
...
Next
Catch ex as Exception
End Try

... you are getting a runtime exception in the application? Or is it breaking in debug mode (because you have 'break on exception' turned on)?

aljeff at 24-Jan-13 12:06pm
   
I tried for HKLM first. It worked but I didn't load all the subkey of HKLM.

Here's what I did as per your instructions.


Private Function CreateNodes(ByVal vParentNode As TreeNode, ByVal vRegKey As RegistryKey) As TreeNode

Try

For Each vSubKeyName As String In vRegKey.GetSubKeyNames()
Dim vSubKey As RegistryKey = vRegKey.OpenSubKey(vSubKeyName, RegistryKeyPermissionCheck.Default, Security.AccessControl.RegistryRights.ReadKey)
Dim vChildNode As New TreeNode(vSubKeyName)
vChildNode = CreateNodes(vChildNode, vSubKey)
vParentNode.Nodes.Add(vChildNode)
Next

Catch ex As SecurityException
Catch ex As Exception

End Try

Return vParentNode

End Function
Jason Gleim at 24-Jan-13 19:52pm
   
Yes... that will happen. When the code you wrote hits a key that it can't read because of an exception, execution will break out of the loop and hit the catch block. No further processing will take place. This is how exceptions are structured... they are exceptions to the normal flow of the code.

That is why I suggested you test the permissions before you attempt to read the value of the key. Then you can gracefully handle the issue by not ever reading the key and generating an exception thereby staying within your loop and moving onto the next key.

Look up the security classes on MSDN and educate yourself on the proper way of doing this. I gave you the namespace and the classes you need to look at... you should be able to take it from there.

Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 454
1 Maciej Los 266
2 KarstenK 210
3 CPallini 208
4 Sascha Lefèvre 190
0 Sergey Alexandrovich Kryukov 10,062
1 OriginalGriff 8,620
2 Sascha Lefèvre 3,609
3 Maciej Los 3,372
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 24 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100