使用VB.Net创建Registry Viewer [英] Creating Registry Viewer using VB.Net

查看:72
本文介绍了使用VB.Net创建Registry Viewer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



有人可以帮助我加快申请加载速度。

我正在创建一个类似于注册表编辑器的程序,但仅用于查看目的。

由于我的Try-catch出现安全问题,程序加载速度太慢。 br $> b $ b

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





或者你有代码库可供使用或借用吗?



问候,

Jeff



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

Regards,
Jeff

推荐答案

您可以尝试在for..next之外移动try..catch代码在您的CreateNodes co中循环de
You could try moving the try..catch code outside of the for..next loop in your CreateNodes code


第一个解决方案是正确的,你应该在循环外移动try..catch以纠正性能问题。



但是,您还可以使用属于System.Security.AccessControl的RegistrySecurity类中的RegistryRights枚举来测试您的应用程序是否在具有足够权限的帐户下运行,以执行您想要执行的操作。 RegistyKey类上还有一个GetAccessControl方法,允许您获取与特定密钥关联的用户权限。



首先检查权限并优雅地失败是一个很大的问题更好的方法,假设一切都会工作,然后尝试捕捉一个catch块的可能性。
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.


这篇关于使用VB.Net创建Registry Viewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆