使用dictionarys和循环VB.net的最佳实践

时间:2013-07-13 19:37:14

标签: vb.net .net-2.0 vb.net-2010

我正在尝试编写一个程序来检查注册表中Windows服务的起始值,我正在使用Dictionary(String,Point)来存储注册表中的服务名称(String)和Default和起始值(点)X的真正起始值为默认值,Y为真。

Private _defaultRegistryVals As New Dictionary(Of String, Point)

Public Enum StartupMode
        Disabled = 0
        Manual = 1
        Auto = 2
        AutoDelayed = 3
    End Enum

Private Function Analyze as Boolean
Dim mostOfRegistryPath As String = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\"
 If Not _HasLoaded then

_defaultRegistryVals.Add("AxInstSV", New Point(StartupMode.Manual, 0)) 
_defaultRegistryVals.Add("SensrSvc", New Point(StartupMode.Manual, 0))
_defaultRegistryVals.Add("AeLookupSvc", New Point(StartupMode.Manual, 0))

_HasLoaded = True

End If

For Each kvp As KeyValuePair(Of String, Point) In _defaultRegistryVals

Try 

kvp.Value.Y = TryCast( My.Computer.Registry.GetValue (mostOfRegistryPath + kvp.Key),      "Start", Nothing), double)
Catch ex As Exception

End Try

Next kvp 

由于Dictionary是只读的,我的程序从注册表中获取每个Windows服务的Start Value的真值的最佳方法是什么。我应该检查每个值作为添加词典条目吗?还是有更好的方法?

这不是整个代码,而是它在某种程度上工作所需的snippits。大多数其他代码只是整个服务列表,以及检查Windows版本的按钮,然后开始分析注册表。

1 个答案:

答案 0 :(得分:1)

尝试以下代码

    Dim DictionaryKeys1(_defaultRegistryVals.Count - 1) As String

    _defaultRegistryVals.Keys.CopyTo(DictionaryKeys1, 0)

    For Each KeyValue In DictionaryKeys1
        Try
            Dim RegistryValue1 As String = CStr(My.Computer.Registry.GetValue(mostOfRegistryPath + KeyValue, "Start", Nothing))

            If Not RegistryValue1 Is Nothing Then
                Dim Point1 As New Point

                Point1.X = _defaultRegistryVals(KeyValue).X
                Point1.Y = CInt(RegistryValue1)

                _defaultRegistryVals(KeyValue) = Point1
            End If
        Catch ex As Exception
            MsgBox("Error")
        End Try
    Next