Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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
Updated 24-Jan-13 6:25am
v2

You could try moving the try..catch code outside of the for..next loop in your CreateNodes code
 
Share this answer
 
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.
 
Share this answer
 
Comments
aljeff 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 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900