'有時候需要掃描遠端電腦的註冊表,判斷一些鍵值項的存在與否,或修改他們
'.NET 裡的Microsoft.Win32命名空間下的RegistryKey ,Registry,RegistryHive就是用來操作登錄的
'|______ ScanRemoteRegister ___________|
'| Coypright wgscd (c)2005 |
'| QQ:153964481 E-mail:[email protected] |
'| Blog:http://blog.csdn.net/wgsnet |
'|______________________________________|
Dim treeV As New TreeView
Dim SubNode As New TreeNode
Dim treeN As New TreeNode
Function OpenRemoteRegister(ByVal RemoteBaseKey As String, ByVal ComputerName As String) As String
Dim subkey As Microsoft.Win32.RegistryKey
Dim k As Microsoft.Win32.RegistryKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(RemoteBaseKey, Net.Dns.GetHostByName(ComputerName).HostName)
treeV.Nodes.Add(Net.Dns.GetHostByName(ComputerName).HostName) '將電腦名稱加入TreeView
treeV.Nodes.Add(k.Name)
Dim s As String
For Each s In k.GetSubKeyNames '得到子鍵項Name
treeN.Nodes.Add(s)
subkey = k.OpenSubKey(s)
OpenKeys(subkey) '開啟子項目/鍵
Next
treeV.Nodes.Add(treeN)
End Function
Sub OpenKeys(ByVal Key As Microsoft.Win32.RegistryKey)
'k.SubKeyCount'得到子鍵項數
Dim s As String
Dim SubKey As Microsoft.Win32.RegistryKey
treeN.Nodes.Add(Key.Name)
If Key.GetSubKeyNames.Length > 0 Then
For Each s In Key.GetSubKeyNames '得到子鍵項Name
treeN.Nodes.Add(s)
Try
SubKey = Key.OpenSubKey(s, False)
Me.Text = s
If s Like "*microsoft*" Or s Like "*wgscd*" Then '加如搜尋條件Then '加如搜尋條件
'ADD YOR CODE ....
MsgBox(s)
End If
OpenKeys(SubKey)
Catch ex As Exception
End Try
Next
treeV.Nodes.Add(treeN) '將登錄項目以子項新增至TreeView
End If
End Sub
Sub dome()
OpenRemoteRegister(Microsoft.Win32.RegistryHive.CurrentUser, Net.Dns.GetHostByName("wgscd").HostName) '掃描CurrentUser項
'“wgscd”是你要掃描的遠端電腦名,注意,前提是你對“遠端電腦”有相應的權限!
'OpenRemoteRegister(Microsoft.Win32.RegistryHive.LocalMachine, Net.Dns.GetHostByName("wgscd").HostName)'掃描LocalMachine項
' OpenRemoteRegister(Microsoft.Win32.RegistryHive.Users, Net.Dns.GetHostByName("wgscd").HostName) '掃描Users項
' OpenRemoteRegister(Microsoft.Win32.RegistryHive.ClassesRoot, Net.Dns.GetHostByName("wgscd").HostName) '掃描ClassesRoot項
'OpenRemoteRegister(Microsoft.Win32.RegistryHive.CurrentConfig, Net.Dns.GetHostByName("wgscd").HostName) '掃描CurrentConfig項
'OpenRemoteRegister(Microsoft.Win32.RegistryHive.DynData, Net.Dns.GetHostByName("wgscd").HostName) '掃描DynData項
End Sub
Sub ScanRemoteRegistry()
Dim Mythread As New Threading.Thread(AddressOf dome)
Mythread.Start()
End Sub
Sub AddTreeViewToForm() '將TreeView加入到Formss上
With treeV
.Width = 400
.Height = 500
End With
Me.Controls.Add(treeV)
End Sub
呼叫:
ScanRemoteRegistry()